title: Mass Push notification gets my server down
body: <p>When I send mass push notification  to my iOS app users (around 100,000), after my PHP page says "Done: messages has ben sent to X users), my server goes down and shutdown, although the page is done of sending, and all users got my message.</p>
  
  <p>Why my server goes down? I only send request to Apple Gateway Server and their server does the hard job.</p>
  
  <p>My script:</p>
  
  <pre><code>$sound = "default"; 
  $passphrase = 'pass';
  $message = 'some message';
  
  $ctx = stream_context_create();
  stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
  
  $fp = stream_socket_client(
      'ssl://gateway.push.apple.com:2195', $err,
      $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
  
  if (!$fp)
      exit("Failed to connect: $err $errstr" . PHP_EOL);
  
  $body['aps'] = array(
      'alert' =&gt; $message,
      'sound' =&gt; $sound,
      'badge' =&gt; 1
      );
  
  $payload = '{"aps":{"alert":"' . $message . '","sound":"' . $sound  . '","badge":1}}';
  
  $results = mysql_query ("Select * from tokens");
  while ($row = mysql_fetch_array($results)) {
    $deviceToken = $row[Token];
    $i ++;
  
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    $result = fwrite($fp, $msg,strlen($msg));  
  
    if (!$result) {
      fclose($fp);
      sleep (3);
      $fp = stream_socket_client(
      'ssl://gateway.push.apple.com:2195', $err,
      $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
      if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL);
    }
  
  }
  
  
  fclose($fp);
  echo "Done: messages has ben sent to $i users.";
  
  }
  </code></pre>
  
  <p>I have VPS with 2GB RAM.</p>
  
  <p>Any ideas what's causing this issue, and a way to avoid it?</p>