title: Unable to push notifications to iPhone in .NET - .PEM certificate problem?
body: <p>I'm attempting to push notifications to my iPhone, which I've successfully done using PHP.  However, I'm unable to open a socket connection to Apple's push servers using C#.  I don't think there's a problem with the certificate itself, since I'm able to successfully push notifications using this code in PHP:</p>
  
  <pre><code>$ctx = stream_context_create();
  
  stream_context_set_option($ctx, 'ssl', 'local_cert', 'cert.pem');
  
  $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  
  fwrite($fp, $msg);
  </code></pre>
  
  <p>When I try to authenticate in C# using the code below, Apple closes the connection right away.  I get this exception: "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."</p>
  
  <pre><code>sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Default, false);
  </code></pre>
  
  <p>I basically took all my code from this question: <a href="https://stackoverflow.com/questions/1056083/c-iphone-push-server">C# iPhone push server?</a></p>
  
  <p>I thought it might be a problem with the certificate since I don't think C# allows you to just load .PEM files programmatically.  I used OpenSSL to convert it to a .PFX file, but I still have the same problem.</p>
  
  <p>Any idea how I can troubleshoot this (possible certificate) problem?</p>