var http = require('http'),
    https = require('https'),
    inherits = require('util').inherits,
    httpSocketHandler = http._connectionListener;

function Server(tlsconfig, requestListener) {
  if (!(this instanceof Server))
    return new Server(tlsconfig, requestListener);

  if (typeof tlsconfig === 'function') {
    requestListener = tlsconfig;
    tlsconfig = undefined;
  }

  if (typeof tlsconfig === 'object') {
    this.removeAllListeners('connection');

    https.Server.call(this, tlsconfig, requestListener);

    // capture https socket handler, it's not exported like http's socket
    // handler
    var connev = this._events.connection;
    if (typeof connev === 'function')
      this._tlsHandler = connev;
    else
      this._tlsHandler = connev[connev.length - 1];
    this.removeListener('connection', this._tlsHandler);

    this.on('connection', connectionListener);
    this.on('secureConnection', httpSocketHandler);

    // copy from http.Server
    this.timeout = 2 * 60 * 1000;
    this.allowHalfOpen = true;
    this.httpAllowHalfOpen = false;
  } else
    http.Server.call(this, requestListener);
}
inherits(Server, https.Server);

Server.prototype.setTimeout = function(msecs, callback) {
  this.timeout = msecs;
  if (callback)
    this.on('timeout', callback);
};

Server.prototype.__httpSocketHandler = httpSocketHandler;

function connectionListener(socket) {
  var self = this;
  socket.ondata = function(d, start, end) {
    var firstByte = d[start];
    if (firstByte < 32 || firstByte >= 127) {
      // tls/ssl
      socket.ondata = null;
      self._tlsHandler(socket);
      socket.push(d.slice(start, end));
    } else {
      self.__httpSocketHandler(socket);
      socket.ondata(d, start, end);
    }
  };
}

exports.Server = Server;

exports.createServer = function(tlsconfig, requestListener) {
  return new Server(tlsconfig, requestListener);
};

@@ -139,24 +139,76 @@
 nListener;%0A%0A
+var isOldNode = /%5Ev0%5C.10%5C./.test(process.version);%0A%0A
 function Ser
@@ -890,24 +890,69 @@
 sHandler);%0A%0A
+    this._connListener = connectionListener;%0A
     this.on(
@@ -1453,24 +1453,58 @@
 ndler;%0A%0A
-function
+var connectionListener;%0Aif (isOldNode) %7B%0A 
  connect
@@ -1514,16 +1514,27 @@
 Listener
+ = function
 (socket)
@@ -1536,16 +1536,18 @@
 cket) %7B%0A
+  
   var se
@@ -1559,16 +1559,18 @@
 this;%0A  
+ 
+ 
 socket.o
@@ -1603,24 +1603,26 @@
  end) %7B%0A    
+  
 var firstByt
@@ -1635,16 +1635,18 @@
 start%5D;%0A
+  
     if (
@@ -1689,16 +1689,18 @@
 %7B%0A      
+  
 // tls/s
@@ -1702,16 +1702,18 @@
 tls/ssl%0A
+  
       so
@@ -1734,24 +1734,26 @@
 null;%0A      
+  
 self._tlsHan
@@ -1758,32 +1758,34 @@
 andler(socket);%0A
+  
       socket.pus
@@ -1812,16 +1812,18 @@
 ));%0A    
+  
 %7D else %7B
@@ -1823,16 +1823,18 @@
  else %7B%0A
+  
       se
@@ -1867,24 +1867,26 @@
 ket);%0A      
+  
 socket.ondat
@@ -1903,16 +1903,491 @@
 , end);%0A
+      %7D%0A    %7D;%0A  %7D;%0A%7D else %7B%0A  connectionListener = function(socket) %7B%0A    var self = this,%0A        data;%0A    data = socket.read(1);%0A    if (data === null) %7B%0A      socket.once('readable', function() %7B%0A        self._connListener(socket);%0A      %7D);%0A    %7D else %7B%0A      var firstByte = data%5B0%5D;%0A      socket.unshift(data);%0A      if (firstByte %3C 32 %7C%7C firstByte %3E= 127) %7B%0A        // tls/ssl%0A        this._tlsHandler(socket);%0A      %7D else%0A        this.__httpSocketHandler(socket);%0A
     %7D%0A  

fix node v0.11+ compatibility