I haven't used this hand made server very much, but what you are saying about the delay in closing the socket sounds like it could happen.
There may be a blocking command that I am unfamiliar with to check whether the port is free. Otherwise I would think of putting a sleep command in there to give the os time to free it.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.