title: PHP Socket connection through proxy
body: <p>I'm building a webmail-client-like application, and I'm having issues connecting to POP3 through our beloved corp proxy. </p>
  
  <p>Due to client requirements, I'm obligated to use sockets to talk with their POP3 server (they actively specified this is mandatory and they also explained they have a proxy between the app server and the POP) so besides enabling me to work this is kind of the only option I have.</p>
  
  <p>Does anyone know how to do this? </p>
  
  <p>UPDATED TO ADD:</p>
  
  <p>Here's my socket handler:</p>
  
  <pre><code>&lt;?php
  class Socket {
    protected $handle = null;
    protected static $status = null;
    public function __construct($server, $port){
      if(!$this-&gt;connect($server, $port)){
        throw new UnexpectedValueException("Server $server in port $port unreachable.");
      }
    }
    public static function getStatus(){
      return self::$status;
    }  
    public function write($string){
      fputs($this-&gt;handle, $string);
      return $this;
    }
    public function read($lines = 1){
      $lines = abs((int)$lines);//people can be morons
      for($i = 0; $i &lt; $lines; $i++){
        $response []= $this-&gt;getLine();
      }
      if($lines==1){
        $response = $response[0];
      }
      return $response;
    }
    public function connect($server, $port){
      $errNo = 0;
      $handle = fsockopen($server, $port);
      if(!$handle){
        return false;
      }
      $this-&gt;handle = $handle;
      return $this;
    }
    public function disconnect(){
      if(gettype($this-&gt;handle)=='resource'){
        fclose($this-&gt;handle);
      }
    }
    public function __destruct(){
      $this-&gt;disconnect();
    }
    protected function getLine(){
      return fgets($this-&gt;handle);
    }
  }
  </code></pre>
  
  <p>I'm using it like so: </p>
  
  <pre><code>$plug = new Socket('mx.mailserver.com',110);
  $plug-&gt;write("USER $username\n")-&gt;read();
  $could = $plug-&gt;write("PASS $password\n")-&gt;read();
  if(strpos($could,'+OK')!==false){
    $mails = $plug-&gt;write("STAT\n");
  }
  </code></pre>
  
  <p>And right now our proxy is not letting me out; situation I would like to know how to revert.</p>