title: PHP socket confusion
body: <p>I am trying to understand PHP socket use for a specific scenario that I have previously used Perl to solve but would like to be able to complete the same process with PHP instead as part of a PHP project.</p>
  <p>The equipment that I am connecting to will send out data on a specific port at any time and not to any specific address but it accepts 5 connections at any one time. That being the case, I need the PHP script to remain connected to the port while it accepts any data and displays this (for now)</p>
  <p>I have got this code so far and it works to the point that I can get the connection and I can display a single data stream but once that stream end the connection drops:</p>
  <pre><code>$ini_array = parse_ini_file(&quot;config.ini&quot;);
  $host=$ini_array[pbx];
  $port=$ini_array[port];
  
  echo nl2br(&quot;Connected to the $host on port $port\n&quot;);
  
  $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die(&quot;Could not create socket\n&quot;);
  $result = socket_connect($socket, $host, $port) or die(&quot;Could not connect to server\n&quot;); 
  $result = socket_read ($socket, 1024) or die(&quot;Could not read server response\n&quot;);
  echo $result.PHP_EOL;
  </code></pre>
  <p>The Perl solution that works for me includes a while loop but I can't figure out how to do this same thing in PHP:</p>
  <pre><code>my $cfg = new Config::Simple();
  $cfg-&gt;read('config.ini');
  my $host = $cfg-&gt;param(&quot;pbx&quot;);
  my $port = $cfg-&gt;param(&quot;port&quot;);
  print &quot;Connecting to the PBX $host on port $port\n&quot;;
  my $sock = new IO::Socket::INET(PeerAddr =&gt; $host, PeerPort =&gt; $port,Proto =&gt; &quot;tcp&quot;,)
  or die &quot;Cannot connect at address: $host port: $port: $!&quot;;
  
  while (&lt;$sock&gt;) {
  s/^\0+//; # Remove leading null characters
  print $_;
  chomp ($_);
  #$_ =~ s/^[^ ]+//;
  if ($_ =~m&quot;/&quot;) {
  &amp;TXTout;
  }
  </code></pre>
  <p>Any suggestions would be welcomed!</p>