title: C# Client/Server socket will not connect again after it has been disconnected
body: <p>I am building a server and client based application that communicates via sockets (typical stuff).</p>
  
  <p>The intended functionality, is that the client connects, they send some data back and forth, and then the client closes the connection. Some time later, the client reconnects, they send some more information back and forth, and it disconnects again.</p>
  
  <p>The first step in the process works fine. The client connects, BeginAccept fires off as desired, and they talk for a while. The connection appears to close just fine (Disconnect()), and the logic runs through and calls BeginAccept again. However, if the client attempts to connect again, AcceptCallback will not trigger.</p>
  
  <p>Other questions/answers online suggest that the socket needs to be reinstated, but I do that here.</p>
  
  <p>In the real version (trimmed for here) I have extensive exception handling and logging yet there are no exceptions or errors to work with.</p>
  
  <p>If I restart the service, it all works again (but only once), which leads me to believe it is my server code and not my client.
  It appears that the AsyncCallback is simply never triggered again.</p>
  
  <p><strong>What am I doing wrong so that I cannot connect more than once?</strong>
  Thanks!</p>
  
  <p>On the server, in a thread I have the following (this gets called at the start, as well as when the disconnect signal is sent from the client):</p>
  
  <pre><code>try
  {
      //check connected, and if not connected, then close socket and reopen
      if (mDisconnectFlag == true)
      {
          mDisconnectFlag = false;
          this.Disconnect();
      }
  
      if (listener == null || listener.Connected == false)
      {
          // Create a TCP/IP socket.
          this.listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          this.listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  
          // Bind the socket to the local endpoint and listen for incoming connections.
          this.listener.Bind(localEndPoint);
          this.listener.Listen(10);
          // Start an asynchronous socket to listen for connections.
          this.listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
      }
      else if (listener.Connected == false || mAcceptCallBackFlag == true)
      {
          // Start an asynchronous socket to listen for connections.
          this.listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
  
      }
      // Wait until a connection is made before continuing.
      _retevent = ProcessSocketEvent.WaitOne(mSocketProcessTimeInterval, false);
  }
  catch (Exception ex)
  {
      //logging - don't worry about this
  }
  </code></pre>
  
  <p>The disconnection is as follows:</p>
  
  <pre><code>public void Disconnect()
  {
      try
      {
          this.listener.Shutdown(SocketShutdown.Both);
          this.listener.Disconnect(false);
          this.listener.Dispose();
          listener = null;
      }
      catch (Exception ex)
      {
          //logging
      }
  
  }
  </code></pre>
  
  <p>Finally, the call back:</p>
  
  <pre><code>public void AcceptCallback(IAsyncResult ar)
  {
      try
      {
          // Get the socket that handles the client request.
          Socket listener = (Socket)ar.AsyncState;
          Socket handler = listener.EndAccept(ar);
          this.listener = handler;
  
          // Create the state object.
          StateObject state = new StateObject();
  
          handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
              new AsyncCallback(ReadCallback), state);
          mAcceptCallBackFlag = true;
      }
      catch (Exception ex)
      {
          //logging
      }
  
  }
  </code></pre>