title: PHP pfsockopen in a session
body: <p>I'm making an MSN client in PHP. I have this code, which connects to a server and logs in to MSN:</p>
  
  <pre><code>$server2 = explode(":", $xfr[3]);
  $socket2 = pfsockopen($server2[0], (int)$server2[1]);
  $_SESSION["socket"] = $socket;
  echo '&lt;b&gt;Connected to 2nd server.&lt;/b&gt;&lt;br /&gt;';
  //Send MSNP version
  fputs($socket2, "VER 0 MSNP10 CVR0\r\n");
  echo fread($socket2, 5000) . '&lt;br /&gt;';
  //Send user-agent
  fputs($socket2, "CVR 1 0x0409 php ".phpversion()." i386 MSNMSGR 7.0.0000 MSMSGS ".$_POST["username"]."\r\n");
  echo fread($socket2, 5000) . '&lt;br /&gt;';
  //Send username
  fputs($socket2, "USR 2 TWN I ".$_POST["username"]."\r\n");
  //Read USR
  $usr = fread($socket2, 5000);
  echo $usr . '&lt;br /&gt;';
  $usr = explode(" ", $usr);//This is for later usage.
  </code></pre>
  
  <p>Now I need to use this socket in another page (<code>AJAX/status.php</code>). php.net says that the connection stays available. However, this is status.php (just ignore $_SESSION["cid"]), which is called via AJAX:</p>
  
  <pre><code>&lt;?php
  session_start();
  fputs($_SESSION["socket"], "CHG 12 " . $_GET["s"] . " " . $_SESSION["cid"], 5000);
  echo fread($_SESSION["socket"]);
  </code></pre>
  
  <p>Which should change the status. I get this error:</p>
  
  <pre><code>&lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  fputs(): supplied argument is not a valid stream resource in &lt;b&gt;C:\wamp\apps\msnphp\AJAX\status.php&lt;/b&gt; on line &lt;b&gt;3&lt;/b&gt;&lt;br /&gt;
  &lt;br /&gt;
  &lt;b&gt;Warning&lt;/b&gt;:  Wrong parameter count for fread() in &lt;b&gt;C:\wamp\apps\msnphp\AJAX\status.php&lt;/b&gt; on line &lt;b&gt;4&lt;/b&gt;&lt;br /&gt;
  </code></pre>
  
  <p>My socket is persistent, and it's id is saved in session variable $_SESSION["socket"]. I do not understand why it does not work.</p>
  
  <p>I use Windows XP Professional SP2 as server, with WAMP (Apache, MySQL and PHP) server.</p>
  
  <p>Can anyone help me? Thanks!</p>