How to reconnect socket to the server if the socket wasn't disconnected/closed by the server?
<p>I have an TCP asynchronous project and there's a problem when it comes to reconnect the client to the server if the connection was closed.(wasn't closed by the server).</p>

<p>When the server is closed the client gets the following error : </p>

<pre><code>An existing connection was forcibly closed by the remote host at EndReceive(IAyncResult ar) at ReceiveCallback(IAsyncResult ar)
</code></pre>

<p>PS:The client can only receive streams from the server.</p>

<p>Things being said , I understand that I need somehow to reconnect to the server .
I have tried to do something but it didn't worked because the client was not responding and exiting.</p>

<p>What I did was , in the catch block of the Receiving callback which contains the steps below , I set the ClientSocket to Disconnected(true) and in a while loop (!ClientSocket.Connected) , I was recalling the main method which starts the asynchronous operation again and the last line of code was Thread.Sleep(n);</p>

<p>These things are done in the receive callback</p>

<ol>
<li>Getting the string from the buffer</li>
<li>Doing whatever.. with the string</li>
<li>Calling again BeginReceive</li>
</ol>

<p>PS: I don't want to use another port.</p>
