title: Perl IO::Socket timing problem
body: <p>I've bumped into a strange problem. I wrote a little daemon in Perl which binds a port on a server. 
  On the same server there's a LAMP running and the client for my Perl daemon is a php file that opens a socket with the daemon, pushes some info and then closes the connection. In the Perl daemon I log each connection in a log file for later usage.</p>
  
  <p>My biggest problem is the following: between the moment when the php script finishes its execution there are 15-20seconds until the daemon logs the connection.</p>
  
  <p>PHP Client:</p>
  
  <pre><code>$sh = fsockopen("127.0.0.1", 7890, $errno, $errstr, 30);
  if (!$sh) 
  {
  echo "$errstr ($errno)&lt;br /&gt;\n";
  } 
  else 
  {
  
      $out = base64_encode('contents');
      fwrite($sh, $out);
      fclose($sh);
  }
  </code></pre>
  
  <p>Perl daemon (just the socket part)</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  use Proc::Daemon;
  use Proc::PID::File;
  use IO::Socket;
  use MIME::Base64;
  use Net::Address::IP::Local;
  
  MAIN:
  {
  #setup some vars to be used down...
  if (Proc::PID::File-&gt;running())
          {
                  exit(0);
          }
  
  
          my $sock = new IO::Socket::INET(
                          LocalHost =&gt; $ip,
                          LocalPort =&gt; $port,
                          Proto =&gt; 'tcp',
                          Listen =&gt; SOMAXCONN,
                          Reuse =&gt; 1);
  
          $sock or die "no socket :$!";
          my($new_sock, $c_addr, $buf);
  
  for (;;) 
          {
  
                  # setup log file 
                  open(LH, "&gt;&gt;".$logs);
  
                  print "SERVER started on $ip:$port \n";
                  print LH "SERVER started on $ip:$port \n";
  
                  while (($new_sock, $c_addr) = $sock-&gt;accept())
                  {
                          my ($client_port, $c_ip) =sockaddr_in($c_addr);
                          my $client_ipnum = inet_ntoa($c_ip);
                          my $client_host =gethostbyaddr($c_ip, AF_INET);
  
                          my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
                          $year += 1900;
                          $mon += 1;
                          print "$year-$mon-$mday $hour:$min:$sec [".time()."] - got a connection from: [$client_ipnum]";
  
                          open(AL, "&gt;&gt;".$accessLog);
                          print AL "$year-$mon-$mday $hour:$min:$sec [".time()."] - got a connection from: [$client_ipnum]\n";
                          close AL;
  
                          while (defined ($buf = &lt;$new_sock&gt;))
                          {
                                  print "contents:",  decode_base64($buf), " \n";
                                  open(FH, "&gt;".$basepath."file_" . time() .".txt")  or warn "Can't open ".$basepath."file_".time().".txt for writing: $!";
                                  print FH decode_base64($buf);
                                  close FH;
                          }
                  }
                  close LH;
  
          }
  
  }
  </code></pre>
  
  <p>What is the thing that I do so wrong and then leads to 20seconds gap between php closing the socket after writing it and the Perl script logging the connection. Any idea? 
  Be gentle, I'm new to Perl :)</p>