title: PHP Sockets - can only connect from localhost (Port Forwarding problem?)
body: <p>First of all, thanks for taking the time to read this. I have a strange problem with PHP sockets. I working on a php socket daemon which works via localhost, but when I try to connect from outside the LAN or another PC, it doesn't work. I've simplified my daemon to a very basic socket connection to replicate the issue for you to see.</p>
  
  <p>Basically, here's the senario. I start the socket daemon on my server on port 6667. I can connect to the daemon via telnet and from the browser on the local machine running the daemon, but I cannot from any other machine - the daemon doesn't even see a connection attempt being made.</p>
  
  <p>To further complicate the issue (which is why I think it's a port forwarding issue), my ISP blocks port 80, so I've setup dyndns and my router to use port 8000. I've also setup my router to forward port 6667 to my server.</p>
  
  <p>To access my daemon from a browser, I enter the following (seudo) url:</p>
  
  <pre><code>http://mydomain.com:8000/client.php
  </code></pre>
  
  <p>This works from the local machine and will connect, but from any other machine, the daemon doesn't even see a connection attempt being made. However, if I specify the port like this:</p>
  
  <pre><code>http://mydomain.com:6667
  </code></pre>
  
  <p>my daemon does see a connection being made, but of course then the browser doesn't have a client page loaded that the user can use to interact with the daemon.</p>
  
  <p>My client uses a flash file to create the socket connection (jsocket), but I know it isn't the cross-domain policy file because the policy is correct, and when connecting via localhost, it serves the policy file correctly.</p>
  
  <p>Here's the simplified daemon code:</p>
  
  <pre><code> &lt;? 
  
  // set some variables 
  $host = '0.0.0.0'; 
  $port = 6667; 
  
  // 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 input back 
  $output = $input . "\n"; 
  socket_write($spawn, $output, strlen ($output)) or die("Could not write output\n"); 
  
  // close sockets 
  socket_close($spawn); 
  socket_close($socket); 
  ?&gt;
  </code></pre>
  
  <p>Summary:</p>
  
  <p>I CAN connect from localhost via telnet and browser... I CAN connect from other machines via telnet, but I CAN NOT connection from the browser from other machines using the ip or domain name when port 8000 is specified. The daemon doesn't see any connection attempt. If I specify port 6667, then the daemon see's a connection attempt, but that is useless to the user. :(</p>
  
  <p>Any help in this matter would be greatly appreciated! Thanks!</p>