I\'m trying to reuse a socket in an asynchronous HTTP client, but I\'m not able to connect to the host the second time around. I basically treat my asynchronous HTTP client
I commented on this question: what is benefit from socket reuse in C# about socket reuse using Disconnect(true)/DisconnectEx()
and this may help you.
Personally I think it's an optimisation too far in client code.
Re update 1 to your question; no, you'd get an AbortedOperation exception if that were the case (see here: VB.NET 3.5 SocketException on deployment but not on development machine) and the docs are wrong if you're running on Vista or later as it doesn't enforce the "thread must exist until after overlapped I/O completes" rule that previous operating systems enforce.
As I've already said in the reply to the linked question; there's very little point in using this functionality for outbound connection establishment. It's likely that it was originally added to the Winsock API to support socket reuse for AcceptEx()
on inbound connections, where, on a very busy web server that was using TransmitFile()
to send files to clients (which is where disconnect for reused seems to have originated). The docs state that it doesn't play well with TIME_WAIT
and so using it for connections where you initiate the active close (and thus put the socket into TIME_WAIT
, see here) doesn't really make sense.
Can you explain why you think this micro optimisation is actually necessary in your case?
have you checked the MaxConnections Setting? http://msdn.microsoft.com/de-de/library/system.servicemodel.nettcpbinding.maxconnections.aspx