title: Getting 'Address already in use' when I try to bind a socket to localhost in php
body: <p>I'm trying to create a socket connection from a php script to a local server (Qt, QLocalServer) but I'm having trouble just creating a connection on the php side.</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL);
  ini_set('display_errors', 'On');
  
  
  set_time_limit(0);
  ob_implicit_flush();
  
  echo 'usr='. get_current_user().'&lt;br/&gt;';
  
  $address = 'localhost';
  $port = 4444; //Different port numbers produce same result
  
  if (($sock = socket_create(AF_UNIX, SOCK_STREAM, 0)) === false) 
  {
      echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
      exit();
  }
  
   if (socket_bind($sock, $address, $port) === false) 
  {
      echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
      exit();
  }
  
  ...
  </code></pre>
  
  <p>This results in </p>
  
  <blockquote>
    <p>usr=root
    Warning: socket_bind(): unable to bind address [98]: Address already in use in /var/www/nbr/socket.php on line 28 socket_bind() failed: reason: Address already in use </p>
  </blockquote>
  
  <p>I've tried a number of things which give indications of what the problem may be, but not how to resolve it.  socket_getsockname produces garbage when I try to echo the address and port information, but if I change <code>AF_UNIX</code> to <code>AF_INET</code>, and add</p>
  
  <pre><code>$addr = ""; $pt = "";    
  echo "Socket name ok: " . socket_getsockname($sock, &amp;$addr, &amp;$pt) . '&lt;br/&gt;';
  echo $addr .    ", " . $pt . '&lt;br/&gt;';
  </code></pre>
  
  <p>the result is</p>
  
  <blockquote>
    <p>Socket name ok: 1</p>
    
    <p>0.0.0.0, 0</p>
  </blockquote>
  
  <p>So is address/port just never set properly somehow?  Also, subsequent <code>socket_get_option($sock, 0, SO_REUSEADDR)</code> fails with <code>AF_UNIX</code>, succeeds with <code>AF_INET</code> but I still get the address unavailable error.</p>
  
  <p>What am I doing wrong?</p>