It seems that using socket.Close() for a tcp socket, doesn\'t fully close the socket. In the following example I\'m trying to connect to example.com at port 9999, which is not o
By design, you should always call Shutdown
before closing the socket.
mySocket.Shutdown(SocketShutdown.Both);
mySocket.Close();
Doing so effectively disables send and receive on the socket, so it will not be accepting incoming data after you've closed it, even if the OS still has control of it.
Jon Skeet also has a point, that since you're opening the connection asynchronously, it may actually be connecting while you're trying to close it. However, if you call Shutdown
on it, it will not allow information to be received as you are experiencing.
Edit: You can only Shutdown
a socket that is already connected, so bear this in mind as you write your code.