Recently, someone asked this question on the CodeProject forums where the OP wondered if reusing the socket would mean he’d get the same client endpoint. This is the gist of my response to him.
Socket.Disconnect(true) will internally result in Winsock’s
DisconnectEx function being called with the
From the docs:
Prepares the socket handle to be reused. When the DisconnectEx request completes, the socket handle can be passed to the AcceptEx or ConnectEx function.
So it’s only the socket handle that will be reused. This has nothing to do with your local endpoint.
Socket reuse is meant for fairly advanced use, where you know exactly what you are doing. It’s typically used in high performance heavy traffic TCP servers. The idea being that by reusing a socket, you save some time (the time that would normally have gone into allocating that socket and its resources). So what some server developers do is to have a pre-created socket pool that they pull sockets from as and when required.
So in my opinion, for a client side TCP application there is never a good reason to reuse a socket.
In short, always use
Socket.Disconnect(false) unless you really know what you are doing.
For some reason, this is not so well documented and leads to a lot of confusion.