title: Unable to run client server program on the server getting error : socket_bind(): unable to bind address [98]: Address already in use
body: <p>I have created a client and server programs using socket it is working perfectly on the localhost but when i upload it to the server through cpanel it does not work properly and it gives following error. The main thing is some times both of the program works on the server.
  I am new to socket programming and seen some of the related posts on stackoverflow but was unable to found exact solution. </p>
  
  <pre><code>Client: Warning: socket_connect(): unable to connect [111]: Connection refused in .../client.php on line 10
  Could not connect to server
  
  Server:Warning: socket_bind(): unable to bind address [98]: Address already in use in ..server.php on line 10
  Could not bind to socket
  </code></pre>
  
  <p>Client.php</p>
  
  <pre><code>&lt;?php
  // where is the socket server?
  $host    = "XX.XX.XXX.XX";
  $port    = 25763;
  $message = "Hello Server This is the first message to the server";
  echo "Message To server :".$message;
  // create socket
  $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  // connect to server
  $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");  
  // send string to server
  socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
  // get server response
  $result = socket_read ($socket, 1024) or die("Could not read server response\n");
  echo "Reply From Server  :".$result;
  // close socket
  socket_close($socket);
      // print result to browser
  ?&gt;
  </code></pre>
  
  <p>Server.php</p>
  
  <pre><code>&lt;?php
  // set some variables
  $host = "XX.XX.XXX.XX";
  $port = 25763;
  // don't timeout!
  set_time_limit(0);
  // create socket
  $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
  // bind socket to port
  $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
  // start listening for connections
  $result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
  
  // accept incoming connections
  // spawn another socket to handle communication
  $spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
  // read client input
  $input = socket_read($spawn, 1024) or die("Could not read input\n");
  // clean up input string
  $input = trim($input);
  echo "Client Message : ".$input;
  // reverse client input and send back
  $output = strrev($input) . "\n";
  echo $spawn;
  socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n");
  // close sockets
  socket_close($spawn);
  socket_close($socket);
  ?&gt;
  </code></pre>