It is not quite clear what your problem is.
If you shutdown a server that is serving a specific port and you want to restart that server you have to observe some things.
There may be still open connections when shutting down (closing the socket). So the server should close active connections first. Those connections will be then in TIME_WAIT state. That means that they are not closed immediately but after some time: When receiving ACK for the FIN send before to indicate closing or after a time out (default is 2 * MSL / Maximum Segemnt Time which is 2 minutes by default). So you have to wait this time before binding the same port to a new socket or just try it (binding will fail while there are still connections in TIME_WAIT state).
But you can force ignoring of connections in TIME_WAIT state by using the SO_REUSEADDR socket flag which must be set before binding the socket (see
PHP: socket_set_option - Manual[
^] ). This flag should be always set for server sockets. But even when using it, you should close all connections and wait some time before closing the socket. This will hopefully close most connections to clients.