title: Unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)
body: <p>I have a problem, I'd like to send out push notifications using php, however I keep getting this error:</p>
  
  <pre><code>Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/colupon/public_html/iPhone/push/index.php on line 21
  Failed to connect: 111 Connection refused
  </code></pre>
  
  <p>My code is as follows:
      
  
  <pre><code>$deviceToken = '0f************************************************************78';
  
  $passphrase = '************';
  
  $message = 'My first push notification!';
  
  ////////////////////////////////////////////////////////////////////////////////
  
  $ctx = stream_context_create();
  $filename = 'ckdev.pem';
  stream_context_set_option($ctx, 'ssl', 'local_cert', $filename);
  stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  
  // Open a connection to the APNS server
  $fp = stream_socket_client(
  'ssl://gateway.sandbox.push.apple.com:2195', $err,
  $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  
  if (!$fp)
  exit("Failed to connect: $err $errstr" . PHP_EOL);
  
  echo 'Connected to APNS' . PHP_EOL;
  
  // Create the payload body
  $body['aps'] = array(
  'alert' =&gt; $message,
  'sound' =&gt; 'default'
  );
  
  // Encode the payload as JSON
  $payload = json_encode($body);
  
  // Build the binary notification
  $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . 
  
  $payload;
  
  // Send it to the server
  $result = fwrite($fp, $msg, strlen($msg));
  
  if (!$result)
      echo 'Message not delivered' . PHP_EOL;
  else
      echo 'Message successfully delivered'.PHP_EOL;
  
  // Close the connection to the server
  fclose($fp);
  
  ?&gt;
  </code></pre>
  
  <p>I believe the problem is with the server I'm using because I posted the same code and the same .pem file on a different server and it sent the notification without any problem. I attempted to open up ports on the firewall for my server because I read that might cause this problem, but the same error message still showed up. Is there anything else I can do? Any help would be greatly appreciated, thanks!</p>