title: tcp having trouble connecting c#
body: <p>I'm having trouble getting my Sender thread to connect to my Receiver thread. Here's my code below (I'm a novice when it comes to socket programming so if you know a better of sending images over sockets please let me know):</p>
  
  <p>Sender thread:</p>
  
  <pre><code>public void SendSS() 
      {            
          try
          {             
  
              while (!mainFrm.ssStop)
              {
                 TcpClient ssTcpClient = new TcpClient();
                  ssTcpClient.Connect(mainFrm.contactIP, 1500);                    
                  if (ssTcpClient.Connected)
                  {
                      Image screenShotBMP = GrabScreen();
                      NetworkStream ns = ssTcpClient.GetStream();
                      memStream = new MemoryStream();
  
                      screenShotBMP.Save(memStream, ImageFormat.Png);
  
                      byte[] bytesToSend = memStream.ToArray();                       ;
                      ns.Write(bytesToSend, 0, bytesToSend.Length);
                      ns.Flush();
  
                      screenShotBMP.Dispose();
                      ns.Close();
                      memStream.Close();
                      ssTcpClient.Close();
                  }
              }
          }
          catch (Exception ex)
          {
  
              MessageBox.Show(ex.Message, "frmVoiceChat-SendSS()", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
  </code></pre>
  
  <p>Receiver thread:</p>
  
  <pre><code>public void ReceiveSS()
      {
          try
          {
              TcpListener ssTcpListener = new TcpListener(IPAddress.Any, 1500);
              while (!mainFrm.ssStop)
              {
                  ssTcpListener.Start();
                  TcpClient tcpReceiver = ssTcpListener.AcceptTcpClient();
                  //TcpClient tcpReceiver = new TcpClient();
                  ssTcpListener.AcceptTcpClient();
                  //tcpReceiver.Connect(mainFrm.contactIP, 1500);
                  if (tcpReceiver.Connected)
                  {
                      labelText("Connected!!!");
                      //NetworkStream receivedNs = tcpReceiver.GetStream();
                      NetworkStream receivedNs = new NetworkStream(tcpReceiver.Client);
                      //ssTcpListener.Server.Receive();
                      //byte[] imageBytes = new byte[tcpReceiver.ReceiveBufferSize];
                      //receivedNs.Read(imageBytes, 0, imageBytes.Length);
                      //MemoryStream receivedMs = new MemoryStream(imageBytes);
                      //receivedNs.CopyTo(receivedMs);
  
                      //Image image = Image.FromStream(receivedNs); // ArgumentException: Parameter is not valid exception here.                        
                      pbScreenShare.Image = new Bitmap(receivedNs);
  
                      receivedNs.Close();
                      tcpReceiver.Close();
                      ssTcpListener.Stop();
                      //image.Dispose();
                      //receivedMs.Close();
                  }                    
                  else
                  {
                      labelText("Not Connected!!!");
                  }
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message, "frmVoiceChat-ReceiveSS()", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
  </code></pre>