#!/usr/bin/perl

use lib qw(/usr/local/library/perl /usr/local/library/perl/lib/perl5 /usr/local/library/perl/lib/perl5/i386-linux-thread-multi);

use IO::Socket;
use Proc::Fork;

my $server_sock = new IO::Socket::INET (
  LocalHost => '127.0.0.1',
  LocalPort => 2000,
  Listen    => 100,
  Proto     => 'tcp',
  Reuse     => 1,
);

sub main {
  my ($sock) = @_;

  while (<$sock>) {
    print $_;
    chomp();
    last if (!length($_));
  }

  while (1) {
    print $sock 'EXEC Dial SIP/201';
    sleep(2);
  }
}

while (1) {
  my $client_socket = $server_sock->accept();
  print "Client Connected\n";

  run_fork {
    child {
      eval {
        main($client_socket);
      };

      if ($@) {
        print "Client failed: $@\n";
      }

      $client_socket->close();
      exit;
    }
  }
}
