title: C# Socket Exception - No Connection could be made because the target machine actively refused it
body: <p>I've coded a simple program using sockets. My goal is to connect my pc(which will act as server) to the PC of a friend across the web but my friend, being the client, is receiving back the exception in the title</p>
  
  <p>The connection is done on port 51111, which we both have opened and checked for it being open through cmd trying to run the server program on both PCs and finding 0.0.0.0.51111 as LISTENING by using netstat -a.
  We have also disabled the firewall and both client and server are running with administrator privilege on both PCs</p>
  
  <p>The IP in the Client(which was used by my friend on his pc) is the one I(the Server) had at the moment of the test, found by looking on a "My IP" website</p>
  
  <p>Client</p>
  
  <pre><code>class Client
  {
      public void SocketClient(string request)
      {
          int maxBuffer = 1024;
          try
          {
              IPAddress a = new IPAddress(new byte[] { 2,44,31,207 });
              IPEndPoint remoteEP = new IPEndPoint(a, 51111);
  
              Socket sender = new Socket(a.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  
              try
              {
                  sender.Connect(remoteEP);
  
                  Console.WriteLine("Connected to " + sender.RemoteEndPoint.ToString());
  
                  byte[] message = Encoding.ASCII.GetBytes(request);
                  int bytesSent = sender.Send(message);
                  byte[] response = new byte[maxBuffer];
                  int bytesReceived = sender.Receive(response);
                  Console.WriteLine("Received : " + Encoding.ASCII.GetString(response, 0, bytesReceived));
              }
              catch (ArgumentNullException ane)
              {
                  Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
              }
              catch (SocketException se)
              {
                  Console.WriteLine("SocketException : {0}", se.ToString());
              }
              catch (Exception e)
              {
                  Console.WriteLine("Unexpected exception : {0}", e.ToString());
              }
          }
          catch (Exception e)
          {
              Console.WriteLine(e.ToString());
          }
      }
  }
  </code></pre>
  
  <p>Server</p>
  
  <pre><code>class Server
  {
      private Socket listener;
      public void SocketServer()
      {
          int maxBuffer = 1024;
  
          Console.WriteLine("LISTENER STARTED");
          listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
          listener.Bind(new IPEndPoint(IPAddress.Any, 51111));
  
          listener.Listen(5);
          while (true)
          {
              Console.WriteLine("WAITING FOR CONNECTION");
              Socket socket = listener.Accept();
  
              string receivedMessage = string.Empty;
              while (true)
              {
                  byte[] receivedBytes = new byte[maxBuffer];
                  int bytesReceivedCount = socket.Receive(receivedBytes);
                  receivedMessage += Encoding.ASCII.GetString(receivedBytes, 0, bytesReceivedCount);
                  if (receivedMessage.IndexOf('\n') &gt; -1)
                  {
                      break;
                  }
              }
              Console.WriteLine("Message from Client : " + receivedMessage);
              string reply = ("Message received!");
              byte[] replyBytes = Encoding.ASCII.GetBytes(reply);
              socket.Send(replyBytes);
              socket.Shutdown(SocketShutdown.Both);
              socket.Close();
          }
      }
  
      public void ShutDownServer()
      {
          listener.Shutdown(SocketShutdown.Both);
          listener.Close();
      }
  }
  </code></pre>