title: SocketException trying to connect to server "A connection attempt failed because the connected party did not properly respond after a period of time"
body: <p>I have a server, which is simply a program that listens on a port of my computer. I have a client program, to, that connects to this port, sends requests and gets responses. For this I use the <code>System.Net.Socket</code> class, with the tcp protocol.</p>
  
  <p>The problem is that my client can connect when the server is on the same computer, but when I give the client to someone else, it doesn't work. SocketException : A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
  I don't understand, but as I am just beginning, I think something's hiding from me...</p>
  
  <p>Here is my code:</p>
  
  <p>Client:</p>
  
  <pre><code>private void Connect_Click(object sender, EventArgs e)
          {
              try
              {
                  sock.Connect(new IPEndPoint(IPAddress.Parse(ip.Text), 1234));
                  new Thread(() =&gt;
                  {
                      read();
                  }).Start();
              }
              catch (Exception ex)
              {
                  //Manage exception
              }
          }
  
  private void read()
          {
              while (true)
              {
                  try
                  {
  
                      byte[] buffer = new byte[255];
                      int rec = sock.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                      if (rec &lt;= 0)
                          throw new SocketException();
  
                      Array.Resize(ref buffer, rec);
  
                      //Displaying received data
                      Invoke((MethodInvoker)delegate
                      {
                          requests.Items.Add(Encoding.Default.GetString(buffer));
                      });
                  }
                  catch
                  {
                      MessageBox.Show("DISCONNECTED");
                      sock.Close();
                      break;
                  }
              }
  
              Application.Exit();
          }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>private void Listen()
          {
              server.Listen(0);
  
              new Thread(AcceptOneConnection).Start();
          }
  
  private void AcceptOneConnection()
          {
              if (client != null)
                  client.Close();
  
              client = server.Accept();
  
              MessageBox.Show("Client accepted");
  
              while (true)
              {
                  string received = ReceiveData();
  
                  if (received == "stop connection")
                      break;
  
                  string[] request = received.Split(' ');
  
                  switch (request[0])
                  {
                      case "register":
                          Register(request);
                          break;
                      case "login":
                          Login(request);
                          break;
                      case "disconnect":
                          UsersManager.Disconnect();
                          SendData("Disconnected");
                          break;
                      default:
                          SendData("Unknown request");
                          break;
                  }
              }
  
              client.Close();
          }
  
  private string ReceiveData()
          {
              try
              {
                  byte[] buffer = new byte[255];
                  int rec = client.Receive(buffer, 0, buffer.Length, 0);
  
                  if (rec &lt;= 0)
                      throw new SocketException();
  
                  Array.Resize(ref buffer, rec);
  
                  return Encoding.ASCII.GetString(buffer);
              }
              catch
              {
                  return "";
              }
          }
  </code></pre>
  
  <p>What should I change to make someone in possession of the client able to connect to the server?</p>