title: Concurrent Clients issue in C# Sockets
body: <p>I am working on a client server application, Windows Server and Linux Client. I was testing my server with multiple concurrent clients. I tried just 20 concurrent connections from client, and i noticed that some requests were not processed despite all 20 requests were the same. They went into the queue and for some reason when their turn comes client was shutdown (Client connect timeout is 5 sec).</p>
  
  <p>Then I added a Thread.Sleep(1000), to check if it is really asynchronous but then i realized it does not process other request until timeout. Despite the fact </p>
  
  <ol>
  <li>It is asynchronous </li>
  <li>ManualResetEvent was set before going to sleep.</li>
  </ol>
  
  <p>Now I am wondering what Am I missing here, as this happens with concurrent connections mostly?</p>
  
  <pre><code>public static void StartServer(IPAddress ipAddr, int port)
  {
      //IPEndPoint serverEndPoint = new IPEndPoint(ipAddr, port);
      IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Any, port);
      Socket clientListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      try
      {
          clientListener.Bind(serverEndPoint);
          clientListener.Listen(500);
          Console.WriteLine("-- Server Listening: {0}:{1}",ipAddr,port);
          while (true)
          {
              resetEvent.Reset();
              Console.WriteLine("|| Waiting for connection");
              clientListener.BeginAccept(new AsyncCallback(AcceptConnection), clientListener);
              resetEvent.WaitOne();
          }
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message);
      }
  }
  
  
  public static void AcceptConnection(IAsyncResult ar)
  {
      // Get the socket that handles the client request.
      Socket listener = (Socket)ar.AsyncState;
      Socket handler = listener.EndAccept(ar);
      // Signal the main thread to continue.
      resetEvent.Set();
      // Create the state object.
      JSStateObject state = new JSStateObject();
      state.workSocket = handler;
      if (handler.Connected)
      {
          Console.WriteLine("** Connected to: {0}", handler.RemoteEndPoint.ToString());
          state.workingDirectory = JSUtilityClass.CreatetTemporaryDirectry();
          try
          {
              Thread.Sleep(1000);
              Receive(state);
          }
          catch (Exception e)
          {
              handler.Shutdown(SocketShutdown.Both);
              handler.Close();
              Console.WriteLine(e.Message);
          }
      }
  }
  </code></pre>