title: Socket communication take a long long time
body: <p>I have a problem on the Socket communication between a computer and the server. What happens is that I establish communication via socket only if I click on a particular button. In the first communication, everything happens perfectly. If I click again, the code is not executed. I put a breakpoint to see what is happening, and saw that on a given line, he is simply trying to execute the line, but for a long long time. (I always pass the same parameter).</p>
  
  <p><strong>Line that Process for a Long Long time</strong></p>
  
  <p><code>serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);</code></p>
  
  <p><strong>Client Code</strong></p>
  
  <pre><code>    private void Form1_Load(object sender, EventArgs e)
      {
          msg("Client Started");
          clientSocket.Connect("server_ip", 8855);
          //clientSocket.Connect("127.0.0.1", 8873);
          label1.Text = "Client Socket Program - Server Connected ...";
          this.imprimir();
      }
  
      protected void imprimir()
      {
          NetworkStream serverStream = clientSocket.GetStream();
          byte[] inStream = new byte[10025];
  
          //// LINE THAT PROCESS FOR A LONG TIME IN THE SECOND TIME.
          serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
  
          string returndata = System.Text.Encoding.ASCII.GetString(inStream);
          int ponto = returndata.IndexOf('.');
          returndata = returndata.Substring(0, ponto);
          string[] quebraretorno = returndata.Split(';');
  
          ServiceReference1.bematechSoapClient bema = new ServiceReference1.bematechSoapClient();
          string r = bema.InformacoesImovelBematech(quebraretorno[0], quebraretorno[1]);
          int retorno = -1;
  
          retorno = IniciaPorta("COM7");
  
          if (retorno == 1)
          {
              ConfiguraModeloImpressora(7);
              BematechTX(r);
              AcionaGuilhotina(1);
              FechaPorta();
          }
  
          clientSocket.Close();
      }
  </code></pre>
  
  <p><strong>Server Code</strong></p>
  
  <pre><code> public void btnImprimeBematech_Locacao()
          {
              Utilidade.QuebraToken tk = new Utilidade.QuebraToken();
              int Credenciada = Convert.ToInt32(tk.CarregaToken(1, HttpContext.Current.Request.Cookies["token"].Value));
              TcpListener serverSocket = new TcpListener(8855);
              int requestCount = 0;
              TcpClient clientSocket = default(TcpClient);
              serverSocket.Start();
              clientSocket = serverSocket.AcceptTcpClient();
  
              requestCount = 0;
  
              while ((true))
              {
                  try
                  {
                      requestCount = requestCount + 1;
                      NetworkStream networkStream = clientSocket.GetStream();
                      string serverResponse = Request.QueryString["id"] + ";" + Credenciada.ToString() + ".";
                      Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);                          
                      networkStream.Write(sendBytes, 0, sendBytes.Length);
                      networkStream.Flush();                    
                  }
                  catch (Exception ex)
                  {                    
                  }
              }
  
              clientSocket.Close();
              serverSocket.Stop();            
          }
  </code></pre>
  
  <p>Can anyone help me ?</p>