diff --git a/httprl.module b/httprl.module
index f928fe7..e625eba 100644
--- a/httprl.module
+++ b/httprl.module
@@ -330,11 +330,11 @@ function httprl_set_default_options(&$options) {
  */
 function httprl_setup_proxy(&$uri, &$options, $url) {
   // Proxy setup
-  $proxy_server = httprl_variable_get('proxy_server', '');
+  $proxy_server = ($options['method'] != 'CONNECT') ? variable_get('proxy_server', '') : '';
   // Use a proxy if one is defined and the host is not on the excluded list.
   if ($proxy_server && _httprl_use_proxy($uri['host'])) {
     // Set the scheme so we open a socket to the proxy server.
-    $uri['scheme'] = 'proxy';
+    $uri['scheme'] = $uri['scheme'] == 'https' ? 'proxy_https' : 'proxy_http';
     // Set the path to be the full URL.
     $uri['path'] = $url;
     // Since the full URL is passed as the path, we won't use the parsed query.
@@ -377,7 +377,8 @@ function httprl_setup_proxy(&$uri, &$options, $url) {
 function httprl_set_socket($uri, &$options, $proxy_server, &$result) {
   $socket = '';
   switch ($uri['scheme']) {
-    case 'proxy':
+    case 'proxy_http':
+    case 'proxy_https':
       // Make the socket connection to a proxy server.
       $socket = 'tcp://' . $proxy_server . ':' . httprl_variable_get('proxy_port', 8080);
       // The Host header still needs to match the real request.
@@ -610,11 +611,22 @@ function httprl_build_request_string($uri, $options) {
     $path .= '?' . $uri['query'];
   }
 
-  // Assemble the request together. HTTP version requires to be a float.
-  $request = $options['method'] . ' ' . $path . ' HTTP/' . sprintf("%.1F", $options['version']) . "\r\n";
+  // Assemble the request together.
+  switch ($options['method']) {
+    case 'CONNECT':
+      $request = $options['method'] . ' ' . $uri['host'] . ':' . (strlen(trim($uri['port'])) ? $uri['port'] : 443);
+      break;
+    default:
+      $request = $options['method'] . ' ' . $path;
+  }
+  // HTTP version requires to be a float.
+  $request .= ' HTTP/' . sprintf("%.1F", $options['version']) . "\r\n";
+
+  // Add in headers.
   foreach ($options['headers'] as $name => $value) {
     $request .= $name . ': ' . trim($value) . "\r\n";
   }
+  // Add in data.
   $request .= "\r\n" . $options['data'];
 
   return $request;
@@ -731,14 +743,35 @@ function httprl_establish_stream_connection(&$result) {
     $result->fp = FALSE;
   }
 
-  // Report any errors or set the steram to non blocking mode.
+  // Report any errors.
   if (!$result->fp) {
     httprl_stream_connection_error_formatter($errno, $errstr, $result);
   }
-  else {
+  // Special HTTPS connection for use through a proxy.
+  if ($uri['scheme'] == 'proxy_https') {
+    $ssl_works = 0;
+    $counter = 0;
+    // Try 3 times to Enable SSL through proxy HTTPS.
+    while ($ssl_works === 0 && $counter < 3) {
+      $ssl_works = stream_socket_enable_crypto($result->fp, TRUE, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
+      if ($ssl_works === 0) {
+        $counter++;
+        // Wait a second before trying again.
+        sleep(1);
+      }
+    }
+    if (!$ssl_works) {
+      $errno = HTTPRL_REQUEST_ABORTED;
+      $errstr = 'Connection aborted; TLS negotiation failed. Establishing an HTTPS connection through the proxy failed.';
+      $result->fp = FALSE;
+    }
+  }
+  // Set the stream to non blocking mode.
+  if ($result->fp) {
     stream_set_blocking($result->fp, 0);
   }
 
+
   // Record end time.
   $end_time = microtime(TRUE);
   $extra = 0;
