Using using (NetworkStream stream = client.GetStream())
Causes the socket to close.
I believe this is the problem:
using (NetworkStream stream = client.GetStream())
That is automatically closing the stream at the end of the block, which in turn (I believe) closes the socket. The documentation is frankly unclear on this - it says that closing the TcpClient
doesn't close the stream, but it doesn't say for sure about closing the stream...
See more on this question at Stackoverflow