title: TCP Server with large clients will not response in good time
body: <p>i have server application that handle request all received command from clinets,
  every things is ok in response when my clients are blow 1000 after my clients connect more than 1000 this service work slowly and will not response in good time more than 20sec... and after 20sec my clients time out error, i dont want increse time out more than 20sec...
  also if i run this app in seprate port every things ok in more client, i must run in one app, where is my mistake?</p>
  
  <pre><code>static class Program {
      static void Main(string[] args) {
          TcpListener baseListernerNewVer = new TcpListener(new System.Net.IPEndPoint(IPAddress.Any, 1425));
          baseListernerNewVer.Start();
          BeginAccept(baseListernerNewVer);
  
  
           while (true) {
               System.Threading.Thread.Sleep(1);
           }
      }
  }
  
  //Wait For New Clinet
  private static void BeginAccept(TcpListener baseListernerNewVer) {
      baseListernerNewVer.BeginAcceptTcpClient((ar) =&gt; {
          while (true) {
              try {
                  BeginAccept(baseListernerNewVer);
                  break;
              }
              catch(Exception e) {
                  System.Threading.Thread.Sleep(100);
              }
          }
  
          //All Client Will Manage Here
          Manage(baseListernerNewVer.EndAcceptTcpClient(ar));
  
  
      }, baseListernerNewVer);
  }
  
  
  
  public void Manage(TcpClient tcpClient) {
      GC.Collect();
      var onlineClientInfo = new OnlineClientInfo(tcpClient);
      try {
          lock (onlineClientsInfos) {
              onlineClientsInfos.Add(onlineClientInfo);
          }
  
          System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback((object state) =&gt; {
              System.Threading.Thread.Sleep(1000);
              while (tcpClient.Connected &amp;&amp; onlineClientInfo.IsConnect &amp;&amp; service1.Running) {
                  try {
                      if (FeedMeMessage(onlineClientInfo)) {
                          System.Threading.Thread.Sleep(1000);
                      }
                      else {
                          System.Threading.Thread.Sleep(1000);
                      }
                  }
                  catch (Exception e) {
  
                  }
              }
          }));
  
          //Wait from command form clients
          CallGetNextCommand(onlineClientInfo);
  
      }
      catch (Exception e) {
  
      }
      finally {
      }
  }
  
  private void DisconnectFromClient(OnlineClientInfo onlineClientInfo) {
      lock (onlineClientsInfos) {
          onlineClientsInfos.Remove(onlineClientInfo);
          try {
              onlineClientInfo.TcpClient.Close();
          }
          catch {
  
          }
      }
  }
  
  private void ReadNextSocketBuffer(OnlineClientInfo onlineClientInfo, DateTime baseDateAndTime, byte[] buffer, int offset, int length, Action fullBufferFilled) {
      TcpClient tcpClient = onlineClientInfo.TcpClient;
      try {
          tcpClient.Client.BeginReceive(buffer, offset, length, SocketFlags.Partial, (ar) =&gt; {
              int len = 0;
              try {
                  len = tcpClient.Client.EndReceive(ar);
                  if (len == 0) {
                      if (DateTime.Now &gt; baseDateAndTime.AddMinutes(1)) {
                          DisconnectFromClient(onlineClientInfo);
                          return;
                      }
                      System.Threading.Thread.Sleep(1);
                  }
              }
              catch {
                  DisconnectFromClient(onlineClientInfo);
              }
  
              if (offset + len == buffer.Length) {
                  fullBufferFilled();
              }
              else {
                  int newOffset = offset + len;
                  ReadNextSocketBuffer(onlineClientInfo, baseDateAndTime, buffer, newOffset, buffer.Length - newOffset, fullBufferFilled);
              }
          }, null);
      }
      catch {
          DisconnectFromClient(onlineClientInfo);
      }
  }
  
  private void ReadSocketLength(OnlineClientInfo onlineClientInfo, int length, Action&lt;byte[]&gt; readedToLength) {
      byte[] buffer = new byte[length];
      ReadNextSocketBuffer(onlineClientInfo, DateTime.Now, buffer, 0, length, () =&gt; {
          try {
              readedToLength(buffer);
          }
          catch { }
      });
  }
  
  
  
  private void ReadCommandLength(OnlineClientInfo onlineClientInfo, Action&lt;int&gt; length) {
      ReadSocketLength(onlineClientInfo, 4, (commandLength) =&gt; {
          try {
              length(Common.ToInt(commandLength));
          }
          catch { }
      });
  }
  
  private void ReadCommand(OnlineClientInfo onlineClientInfo, int commandLength, Action&lt;byte[]&gt; commandResult) {
      ReadSocketLength(onlineClientInfo, commandLength, (command) =&gt; {
          try {
              commandResult(command);
          }
          catch { }
      });
  }
  
  private void GetNextCommand(OnlineClientInfo onlineClientInfo, Action finishedExecute) {
      ReadCommandLength(onlineClientInfo, (commandLength) =&gt; {
          ReadCommand(onlineClientInfo, 1, (commandHeader) =&gt; {
              if (commandLength == 2 &amp;&amp; (ServerCommandType)commandHeader[0] == ServerCommandType.AckVer1) {
                  ReadCommand(onlineClientInfo, 1, (newCommandHeader) =&gt; {
                      try {
                          byte ackCode = newCommandHeader[0];
                          onlineClientInfo.AckReceived(ackCode);
                      }
                      catch { }
                      finishedExecute();
                  });
              }
              else {
                  ReadCommand(onlineClientInfo, commandLength, (command) =&gt; {
                      try {
                          if (ExecuteCommand(command, onlineClientInfo)) {
                              onlineClientInfo.SendAck(commandHeader[0]);
                          }
                      }
                      catch {
  
                      }
                      finishedExecute();
                  });
              }
          });
      });
  }
  
  private void CallGetNextCommand(OnlineClientInfo onlineClientInfo) {
      TcpClient tcpClient = onlineClientInfo.TcpClient;
  
      Action finishedExecute = null;
  
      finishedExecute = () =&gt; {
          if (tcpClient.Connected &amp;&amp; onlineClientInfo.IsConnect) {
              try {
                  GetNextCommand(onlineClientInfo, finishedExecute);
              }
              catch {
              }
          }
          else {
              DisconnectFromClient(onlineClientInfo);
          }
      };
  
      finishedExecute();
  }
  </code></pre>