title: Php sockets, Apache and CentOS 6.3 gives Permission denied
body: <p>What I am trying to do is running a simple PHP script that checks if a game server is online and gets some info from it. I am running exactly this same script on a local box with WAMP Server where i just uncommented php_openssl.dll and php_sockets.dll and - voila - it was working as expected.</p>
  
  <p>But then came our production environment! I usually work with Debian, but our host decided to install CentOS on our dedicated server because the NIC was failing in Debian, and it's been nothing but trouble since. </p>
  
  <p>I overcame a few issues, and am left with this problem: how do I fix PHP sockets? I read that I needed php-common, so I installed this with:</p>
  
  <pre><code># yum install php-common
  </code></pre>
  
  <p>Then i checked <code>phpinfo()</code> and I got this</p>
  
  <pre><code>'./configure'  '--with-openssl' '--enable-sockets' ...
  </code></pre>
  
  <p>So this is looking good if you ask me, both openssl and sockets are installed and should be working, however this is not the case. I found this script here on Stack Overflow:</p>
  
  <pre><code>&lt;?php
  //just in case
  if (!extension_loaded('sockets')) {
  die('The sockets extension is not loaded.');
  }
  echo '&lt;p&gt;&lt;strong&gt;Establishing connection...&lt;/strong&gt;&lt;/p&gt;';
  $socket = socket_create(AF_INET,SOCK_STREAM,0);
  if (!socket_connect($socket, "stackoverflow.com", 80))
  {
  die('Socket error : '.socket_strerror(socket_last_error()));
  }
  
  echo '&lt;p&gt;&lt;strong&gt;Connection successful!&lt;/strong&gt;&lt;/p&gt;';
  
  $request = join("\n",array(
  "GET / HTTP/1.1",
  "Connection: close",
  "Host: stackoverflow.com",
  "User-Agent: Mozilla/5.0 (Windows NT 6.1)",
  "Accept: text/html,*/*;q=0.8",
  ""));
  socket_write($socket,$request,strlen($request));
  
  $response = socket_read($socket,2048);
  
  
  echo "&lt;p&gt;&lt;strong&gt;This is the received data : &lt;/strong&gt;&lt;/p&gt;";
  echo '&lt;pre&gt;'.htmlentities($response).'&lt;/pre&gt;';
  
  socket_close($socket);
  </code></pre>
  
  <p>What this outputs surprises me completely:</p>
  
  <pre><code>Establishing connection...
  
  Connection successful!
  
  This is the received data :
  
  HTTP/1.0 408 Request Time-out
  Cache-Control: no-cache
  Connection: close
  Content-Type: text/html
  
  &lt;html&gt;&lt;body&gt;&lt;h1&gt;408 Request Time-out&lt;/h1&gt;
  Your browser didn't send a complete request in time.
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>But still, my script is not functioning ? :(
  I have disabled iptables with</p>
  
  <pre><code># service iptables stop
  </code></pre>
  
  <p>just to be sure it's not a firewall issue. What happens is the following, when I open the page instead of a game servers name I get this:</p>
  
  <blockquote>
    <p>Warning: socket_connect(): unable to connect [13]: Permission denied in /var/www/test.php on line 9 &lt;&lt;&lt;</p>
  </blockquote>
  
  <p>This is the code I'm using, it's open source from xPaw, so I take no credit for it:</p>
  
  <pre><code>&lt;?php
  function queryserver( $IP, $Port = 25565, $Timeout = 2 )
  {
  $Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
  Socket_Set_Option( $Socket, SOL_SOCKET, SO_SNDTIMEO, array( 'sec' =&gt; (int)$Timeout, 'usec' =&gt; 0 ) );
  Socket_Set_Option( $Socket, SOL_SOCKET, SO_RCVTIMEO, array( 'sec' =&gt; (int)$Timeout, 'usec' =&gt; 0 ) );
  if( $Socket === FALSE || Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
  {
  return FALSE;
  }
  Socket_Send( $Socket, "\xFE\x01", 2, 0 );
  $Len = @Socket_Recv( $Socket, $Data, 256, 0 );
  Socket_Close( $Socket );
  if( $Len &lt; 4 || $Data[ 0 ] !== "\xFF" )
  {
  return FALSE;
  }
  $Data = SubStr( $Data, 3 );
  $Data = iconv( 'UTF-16BE', 'UTF-8', $Data );
  
  if( $Data[ 1 ] === "\xA7" &amp;&amp; $Data[ 2 ] === "\x31" )
  {
  $Data = Explode( "\x00", $Data );
  return Array(
  'HostName'   =&gt; $Data[ 3 ],
  'Players'    =&gt; IntVal( $Data[ 4 ] ),
  'MaxPlayers' =&gt; IntVal( $Data[ 5 ] ),
  'Protocol'   =&gt; IntVal( $Data[ 1 ] ),
  'Version'    =&gt; $Data[ 2 ]
  );
  }
  $Data = Explode( "\xA7", $Data );
  return Array(
  'HostName'   =&gt; SubStr( $Data[ 0 ], 0, -1 ),
  'Players'    =&gt; isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
  'MaxPlayers' =&gt; isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0,
  'Protocol'   =&gt; 0,
  'Version'    =&gt; '1.3'
  );
  }
  
  $test = queryserver('SERVERADDRESSHERE'); // (i tried multiple, which are all working @wamp)
  $echo = $test['HostName'] . "&lt;&lt;&lt; &lt;br /&gt;";
  echo $echo;
  </code></pre>