PHP APNS Apple Push Notifications shows different response code every request
<p>I have a PHP script that sends push notifications to an Apple APNS server. 
It is supposed to echo the response code <code>485</code>, which shows that the message was successfully sent.  However, when I run the code, it shows a different numeric code each time, and the message is not sent.</p>

<p>Here is the code:</p>

<pre><code>$payload = array(
    'aps' =&gt; array(
        'alert' =&gt; $message,
        'T_ID' =&gt; $data["trip_id"],
        'S' =&gt; $state,
        'Api' =&gt; 2,
        'sound' =&gt; 'default'
));

$body = array();
$body['aps'] = array('alert' =&gt; "request trip");
$body['aps']['notifurl'] = $payload;
$body['aps']['badge'] = 2;
$payload = json_encode($body);
$ret = array(
    "error" =&gt; 0
);

$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', dirname(BASEPATH) . "/uploads/" . $this-&gt;_apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', $this-&gt;_passphrase);
@$apns = stream_socket_client('tls://' . $this-&gt;_apnsHost . ':' . $this-&gt;_apnsPort, $error, $errorString, 200, STREAM_CLIENT_CONNECT, $streamContext);

if (!$apns) {
    //die('Error creating ssl socket ' . $error . ' ' . $errorString);
    $ret["error"] = 1;
    $ret["details"] = 'Error creating ssl socket ' . $error . ' ' . $errorString;
}

$apnsMessage = // Command "1"
    chr(1)
    // Identifier "88"
    . pack('N', 88)
    // Expiry "tomorrow"
    . pack('N', time() + 86400)
    // Token length
    . chr(0) . chr(32)
    // Device token
    . pack('H*', str_replace(' ', '', $deviceToken))
    // Payload length
    . chr(0) . chr(strlen($payload))
    // Actual payload
    . $payload . $payload;

echo fwrite($apns, $apnsMessage);
fclose($apns);
</code></pre>

<p>Why is this not working as expected?</p>