protected function getApnStream($apnURL)
{
if (!isset($this->apnStreams[$apnURL])) {
// No stream found, setup a new stream
$ctx = $this->getStreamContext();
$this->apnStreams[$apnURL] = stream_socket_client($apnURL, $err, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $ctx);
if (!$this->apnStreams[$apnURL]) {
throw new \RuntimeException("Couldn't connect to APN server. Error no {$err}: {$errstr}");
}
// Reduce buffering and blocking
if (function_exists("stream_set_read_buffer")) {
stream_set_read_buffer($this->apnStreams[$apnURL], 6);
}
stream_set_write_buffer($this->apnStreams[$apnURL], 0);
stream_set_blocking($this->apnStreams[$apnURL], 0);
}
return $this->apnStreams[$apnURL];
}