There is a reason that sockets are not used for some time after they are closed. The socket consists of 4 tuples: Source and Dest Port, Source and Dest IP.
Let's say you close the socket a lot and the client was busy sending data to the server. You wait 5 seconds and reopen the server with the same port, and the same client sends data to the same 4 tuples, the server will receive packets with incorrect tcp sequence numbers, and the connections will be reset.
You shoot yourself in the foot :)
This is why the connections have the time_wait status for 2-4 minutes (depending on the distribution) until they are reused. To be clear, I'm talking about SOCKET, not just the tcp listening port.
source share