<?php

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$pid = pcntl_fork();
if ($pid == -1) {
     die('could not fork');
} else if ($pid) {
     echo 'parent started', PHP_EOL;
     for ($i=0; $i != 10; ++$i) {
        usleep(10);
        echo '-';
     }
     pcntl_wait($status);
     echo 'all done', PHP_EOL;
} else {
     echo 'child started', PHP_EOL;

     foreach ([21, 25, 80] as $port) {
        echo "port($port) is ";
        echo @socket_connect ($sock, '127.0.0.1', $port) ? 'open' : 'closed';
        $error = socket_last_error ($sock);
        if ($error) echo ' (', socket_strerror($error), ')';
        echo PHP_EOL;
     }

     echo 'child exiting', PHP_EOL;
}
