too long

PHP version: 5.4.4 on Debian 3.2.54-2 x86_64. I'm using stream sockets. I created simple server and client applications to illustrate my issue:

<?php
$client = false;
$server_sock = stream_socket_server("tcp://127.0.0.1:20015", $errNo, $errorMessage);
if ($server_sock == false) die("Could not bind to socket: {$errNo} - {$errorMessage}"); 
while (true) { 
    if ($client !== false) {
        if (feof($client)) $data = false;
        else $data = stream_get_contents($client);
        if ($data === false) {
            fclose($client);
            $client = false;
            echo "Client disconnected
";
            continue;
        } else if ($data === "") continue;
        echo "Received: " . $data . "
";            
    } else {
        $clients = array($server_sock);
        $num_streams = stream_select($clients, $write = null, $except = null, 0, 200000);
        if ($num_streams > 0) {
            if (in_array($server_sock, $clients)) {
                $client = stream_socket_accept($server_sock);
                if ($client) {
                    if(!stream_set_blocking($client, 0)) echo "stream_set_blocking() error
";
                } else {
                    echo "stream_socket_accept() error
";
                    $client = false;
                }
            }
        }
    }
}
?>

Client code:

<?php
    $client = stream_socket_client("tcp://127.0.0.1:20015", $errNo, $errorMessage);
    if ($client == false) die("Could not connect to server: {$errNo} - {$errorMessage}
");
    if(!stream_set_blocking($client, 0)) echo "stream_set_blocking() error
";

    $tick = true;

    while (true) {
        sleep(2);
        if ($tick) $toSend = "TICK "; else $toSend = "TOCK ";
        $tick = !$tick;
        $toSend .= time();
        $num_bytes = fwrite($client, $toSend);
        if ($num_bytes === false || $num_bytes < strlen($toSend)) { echo "fwrite() error
"; continue; }
        else echo "Data sent: {$toSend}. Bytes: {$num_bytes}
";
    }
    if (fclose($client)) echo "Socket closed
";
    else echo "fclose() error
";
?>

The problem: if I manually shutdown server (to simulate lost connection), first fwrite() call (on the client side) after server shutdown throws no error. Calling fwrite() second time returns 0 and generates PHP notice "PHP Notice: fwrite(): send of 15 bytes failed with errno=32 Broken pipe" as expected. Everything sent with first fwrite() call is lost but client "thinks" that everything is OK. In production environment, after error client will try to reconnect and if it succeed, it will try to resend data. But here client is not fully aware of sitaution. Why fwrite() behaves like this? I was trying to enable blocking mode on the stream with timeout, fwrite() and then get stream meta data, but it seems that fwrite() never times out, even if it gives error. Any tips or solutions?