After closing a TCP port there is a period in which a new connection can not be opened again. This is due to the TIME_WAIT state in the TCP protocol.
This article, titled “TIME_WAIT and its design implications for protocols and scalable client server systems”, explains the issue very well. As a quick solution, the flag SO_REUSEADDR can be used when opening a new socket. Though it does not really release the resource it occupies.