title: Weird C# SocketException
body: <p>While testing my Client-Server program, I encountered a weird exception when trying to connect to the server on a different router:</p>
  
  <p>"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."</p>
  
  <p>The client can connect to the server perfectly in the local network, however it doesn't work when it is over the internet. </p>
  
  <p>I port forwarded port 1250 (the one I'm using), and using SimplePortForwarding (http://www.simpleportforwarding.com/) I verified that the port was open and working. </p>
  
  <p>I based my implementation on this tutorial:
  <a href="http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/" rel="nofollow">http://www.developerfusion.com/article/3918/socket-programming-in-c-part-1/</a></p>
  
  <p>Any idea what is wrong?
  Thanks!</p>
  
  <p>Here is the server listen method:</p>
  
  <pre><code>    public void startListening(int port)
      {
          lock(_locker)
          {
              _listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  
              try
              {
                  // Bind socket to local endpoint, and listen for incoming connections
                  IPEndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, port);
                  _listeningSocket.Bind(ipEndpoint);
                  _listeningSocket.Listen(10);
  
                  waitForNewClient();
  
                  // successfully started listening
                  _isListening = true;
  
              } catch (SocketException e)
              {
                  // failed for some strange reason
                  _isListening = false;
              }
          }
      }
  </code></pre>
  
  <p>Here is the client connect code:</p>
  
  <pre><code>    public String connect(String ipAddress, int port)
      {
          lock(_locker)
          {
              if (!_connecting)
              {
                  _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                  IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);
  
                  try
                  {
                      _socket.Connect(endpoint);
                      _connected = true;
                      waitForData();
  
                      _eventManager.queueEvent(new PlayerJoinedEvent(PlayerJoinedEvent.PLAYER_JOINED, name));
                  } catch (SocketException e)
                  {
                      // Exception is thrown HERE
                      return e.Message;
                  }
              }
          }
  
          return "";
      }
  </code></pre>