title: PHP-socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it
body: <p>I am running a client-server code in php for chatting. I am getting this error:
  <strong>socket_connect(): unable to connect [10061]: No connection could be made because the target machine actively refused it</strong></p>
  
  <p>I have gone through many questions and answers on this problem but still unable to solve this.
  Here is my code :</p>
  
  <p>for user-1:</p>
  
  <blockquote>
    <p>
  </blockquote>
  
  <pre><code>$host   = "127.0.0.1";
  $port_1 = 5001;
  $port_2  = 50002;
  // create socket
  $socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  $socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  $result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");  
  
  
  socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
  socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
  $new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");
  
  
          $result = socket_read ($socket_1, 1024);
  
      if (isset($_POST['submit']))
      {
          $message = $_POST['client_message'];
          socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
      }
      // or die("Could not read server response\n");
      if(!empty($result))
      {
          echo "Reply From Server  :".$result;
      }
      socket_close($socket_1);
      socket_close($socket_2);
      socket_close($new_socket);
  ?
  </code></pre>
  
  <blockquote>
    <p></p>
  </blockquote>
  
  <p>This is for user-2
  
  
  <pre><code>$host   = "127.0.0.1";
  $port_1 = 5001;
  $port_2  = 50002;
  // create socket
  $socket_2 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  $socket_1 = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  $result = socket_connect($socket_2, $host, $port_2) or die("Could not connect to server\n");  
  
  
  socket_bind($socket_1, $host, $port_1) or die("Could not bind to socket\n");
  socket_listen($socket_1, 10) or die("Could not set up socket listener\n");
  $new_socket= socket_accept($socket_1) or die("Could not accept incoming connection\n");
  
  
          $result = socket_read ($socket_1, 1024);
  
      if (isset($_POST['submit']))
      {
          $message = $_POST['client_message'];
          socket_write($socket_2, $message, strlen($message)) or die("Could not send data to server\n");
      }
      // or die("Could not read server response\n");
      if(!empty($result))
      {
          echo "Reply From Server  :".$result;
      }
      socket_close($socket_1);
      socket_close($socket_2);
      socket_close($new_socket);
  ?&gt;
  </code></pre>
  
  <p>please help me figuring out the errors.
  Thank you</p>