             }
         }
         
-        return (NSURL(string: urlPolling)!, NSURL(string: urlWebSocket)!)
+        return (urlPolling, urlWebSocket)
+    }
+    
+    func doPoll() {
+        if self.urlPolling == nil || self.websocket {
+            return
+        }
+        
+        let time = Int(NSDate().timeIntervalSince1970)
+        let req = NSURLRequest(URL: NSURL(string: self.urlPolling! + "&t=\(time)-0&b64=1" + "&sid=\(self.sid)")!)
+        
+        NSURLConnection.sendAsynchronousRequest(req, queue: self.pollingQueue) {[weak self] res, data, err in
+            if self == nil {
+                return
+            }
+            
+            if err != nil {
+                println(err)
+                return
+            }
+            
+            let str = NSString(data: data, encoding: NSUTF8StringEncoding)
+            
+            println(str)
+        }
     }
     
     func open(opts:[String: AnyObject]? = nil) {
         let (urlPolling, urlWebSocket) = self.createURLs(params: opts)
-        let reqPolling = NSURLRequest(URL: urlPolling)
-        
-        NSURLConnection.sendAsynchronousRequest(reqPolling, queue: self.pollingQueue) {[weak self] res, data, err in
-            var err:NSError?
-            if self == nil {
-                return
-            } else if err != nil || data == nil {
-                println("Error")
-                println(err)
-                if !self!.client.reconnecting {
-                    self?.client.tryReconnect(triesLeft: self!.client.reconnectAttempts)
+        
+        self.urlPolling = urlPolling
+        self.urlWebSocket = urlWebSocket
+        let time = Int(NSDate().timeIntervalSince1970)
+        let reqPolling = NSURLRequest(URL: NSURL(string: urlPolling + "&t=\(time)-0&b64=1")!)
+        
+        NSURLConnection.sendAsynchronousRequest(reqPolling,
+            queue: self.pollingQueue) {[weak self] res, data, err in
+                var err:NSError?
+                if self == nil {
+                    return
+                } else if err != nil || data == nil {
+                    println("Error")
+                    println(err)
+                    if !self!.client.reconnecting {
+                        self?.client.tryReconnect(triesLeft: self!.client.reconnectAttempts)
+                    }
+                    return
                 }
-                return
-            }
-            
-            let sub = data.subdataWithRange(NSMakeRange(5, data.length - 5))
-            
-            if let json = NSJSONSerialization.JSONObjectWithData(sub,
-                options: NSJSONReadingOptions.AllowFragments, error: &err) as? NSDictionary {
-                    if let sid = json["sid"] as? String {
-                        self?.sid = sid
+                
+                if let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) {
+                    var mutString = RegexMutable(dataString)
+                    
+                    let parsed = mutString["(\\d*):(\\d)(\\{.*\\})?"].groups()
+                    
+                    if parsed.count == 4 {
+                        let length = parsed[1]
+                        let type = parsed[2]
+                        let jsonData = parsed[3].dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
                         
-                        self?.ws = SRWebSocket(URL: urlWebSocket.URLByAppendingPathComponent("&sid=\(self!.sid)"))
-                        self?.ws?.delegate = self
-                        self?.ws?.open()
+                        if type != "0" {
+                            NSLog("Error handshaking")
+                            return
+                        }
                         
-                    } else {
-                        NSLog("Error handshaking")
-                        return
+                        if let json = NSJSONSerialization.JSONObjectWithData(jsonData!,
+                            options: NSJSONReadingOptions.AllowFragments, error: &err) as? NSDictionary {
+                                if let sid = json["sid"] as? String {
+                                    self?.sid = sid
+                                    
+                                    self?.ws = SRWebSocket(URL: NSURL(string: urlWebSocket + "&sid=\(self!.sid)")!)
+                                    self?.ws?.delegate = self
+                                    //self?.ws?.open()
+                                    
+                                } else {
+                                    NSLog("Error handshaking")
+                                    return
+                                }
+                                
+                                if let pingInterval = json["pingInterval"] as? Int {
+                                    self?.pingInterval = pingInterval / 1000
+                                }
+                        }
                     }
                     
-                    if let pingInterval = json["pingInterval"] as? Int {
-                        self?.pingInterval = pingInterval / 1000
-                    }
-            }
+                    self?.doPoll()
+                    self?.startPingTimer()
+                }
         }
     }
     


class SocketEngine: NSObject, SRWebSocketDelegate {
    unowned let client:SocketIOClient
    private var pingTimer:NSTimer?
    private let pollingQueue = NSOperationQueue()
    private var _polling = true
    private var _websocket = false
    private var websocketConnected = false

        return self._polling
    }
    var sid = ""
    var urlPolling:String?
    var urlWebSocket:String?
    var websocket:Bool {
        return self._websocket
    }
