title: fsockopen socket connection is established in constructor but is null in other methods
body: <p>I'm writing a php IMAP API. The constructor connects to them Gmail server and stores the socket in a variable. When I use the same socket connection in another method, the connection is null. Why does this happen and how can I fix it?</p>
  
  <pre><code>class Imap{
  
      const RESPONSE_SIZE = 4096;
      const LOCAL_HOST = '127.0.0.1';
      const CRLF = "\r\n";
  
      //------------RESPONSE CODES---------//
      const OK = "OK";
      const BAD = "BAD";
      const NO = "NO";
  
      //----------------FLAGS--------------//
      const FLAG_ANSWERED = "\\Answered";
      const FLAG_FLAGGED = "\\Flagged";
      const FLAG_DRAFT = "\\Draft";
      const FLAG_DELETED = "\\Deleted";
      const FLAG_SEEN = "\\Seen";
  
      //------------- PRIVATE VARS ------------//
      private $_connection = NULL;
      private $_number = 0;
      private $_instruction_num;
  
      private $_connected = false;
      private $_authenticated = false;
  
      //------------- PUBLIC VARS ------------//
  
      public $error = array();
  
      function __construct($imap_server, $imap_port)
      {
          $this-&gt;connect($imap_server,$imap_port);
      }
  
      function __destruct()
      {
          if($this-&gt;_connected)
              $this-&gt;logout();
      }
  
      private function connect($imap_server, $imap_port)
      {
          if($this-&gt;_connected == false)
          {
              if($imap_server == NULL)    $imap_server = self::LOCAL_HOST;
              if($imap_port == NULL)      $imap_port = 993;
  
              $this-&gt;_connection = fsockopen($imap_server,$imap_port);
  
              if(empty($this-&gt;_connection))
              {
                  throw new Exception('Connection to server could not be established');
              }
              else $this-&gt;_connected = true;      
  
          }
  
          return $this-&gt;_connected;
      }
  
      function login($username, $password)
      {
          echo 'connection: '.$this-&gt;_connected;
          if(!$this-&gt;_authenticated)
          {
              $instruction = $this-&gt;get_instruction_num();
              fputs($this-&gt;_connection,"$instruction LOGIN $username $password".self::CRLF);
              $response = $this-&gt;get_response($instruction);
  
              switch ($response['code']) {
                  case self::OK:
                      $this-&gt;_authenticated = true;
                      break;
  
                  case self::NO:
                      $this-&gt;_authenticated = false;
                      $this-&gt;error = array('error'=&gt;'Invalid username or password.');
                      break;
  
                  case self::BAD:
                  default: 
                      $this-&gt;_authenticated = false;
                      $this-&gt;error = array('error'=&gt;$response['response']);
                  break;
              }
          }
  
          return $this-&gt;_authenticated;
      }
  
  private function get_response($aInstructionNumber)
      {
  
          $end_of_response = false;
  
          if(empty($this-&gt;_connection))
              die('DEAD');//connection is null here.
  
          while (!$end_of_response)
          {
              $line = fgets($this-&gt;_connection,self::RESPONSE_SIZE);
              $response .= $line.'&lt;br/&gt;';
  
              if(preg_match("/$aInstructionNumber (OK|NO|BAD)/", $response,$responseCode))
                  $end_of_response = true;
          }
  
          return array('code' =&gt; $responseCode[1],
              'response'=&gt;$response);
      } 
  </code></pre>
  
  <p>Used in Mailer.php</p>
  
  <pre><code>class Mailer
  {
      const imap_server = "ssl://imap.gmail.com";
      const imap_port = 993;
      const smtp_server = "ssl://smtp.gmail.com";
      const smtp_port = 465;
  
      private $imap = NULL;
      private $smtp = NULL;
      private $username = "";
      private $password = "";
  
      private $logged_in = false;
  
      function __construct()
      {
          try{
              $this-&gt;imap = new Imap(self::imap_server,self::imap_port);
              $this-&gt;smtp = new Smtp(self::smtp_server,self::smtp_port);
          }catch(Exception $e)
          {
              throw new Exception($e-&gt;getMessage());
          }
      }
  
      function login($username,$password)
      {
          if($username != NULL) $this-&gt;username = $username;
          if($password != NULL) $this-&gt;password = $password;
  
          //$this-&gt;imap = new Imap(self::imap_server,self::imap_port);
          if(!$this-&gt;imap-&gt;login($this-&gt;username,$this-&gt;password))
          {
              $result = array('success'=&gt;0,'message'=&gt;$this-&gt;imap-&gt;error());
          }
          else
          {
              $this-&gt;logged_in = true;
              $result = array('success'=&gt;1,'message'=&gt;'Login successful');
          }
  
          return $result;
      }
  </code></pre>
  
  <p>The code in the logging in page:</p>
  
  <pre><code>    if(!isset($_SESSION['mailer']))
  {
      try{
          $mailer = new Mailer();
      }catch(Exception $e){
          die($e-&gt;getMessage());
      }
  
      $_SESSION['mailer'] = serialize($mailer);
  }
  
  if(isset($_POST['username']) &amp;&amp; isset($_POST['password']))
  {
      //ENCRYPT
      $username = htmlentities($_POST['username']);
      $password = htmlentities($_POST['password']);
      $s_mailer = $_SESSION['mailer'];
      $mailer = unserialize($s_mailer);
  
      $_login = $mailer-&gt;login($username,$password);
      if($_login['success']!=1)
      {
          $feedback = $_login['message'];
      }else
          header('Location: inbox.php');      
  
  }
  </code></pre>