                 self.ws?.send(msg)
             }
         } else {
-            // TODO handle polling
+            self.sendPollMessage(msg)
         }
     }
     
     func sendPing() {
-        if self.websocketConnected {
+        if self.websocket {
             self.ws?.send(PacketType.PING.rawValue)
-        }
+        } else {
+            let time = Int(NSDate().timeIntervalSince1970)
+            var req = NSMutableURLRequest(URL: NSURL(string: self.urlPolling! + "&t=\(time)-0&b64=1" + "&sid=\(self.sid)")!)
+            let postStr = "1:\(PacketType.PING.rawValue)"
+            let postData = postStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
+            let postLength = "\(postData.length)"
+            
+            req.HTTPMethod = "POST"
+            req.setValue(postLength, forHTTPHeaderField: "Content-Length")
+            req.setValue("application/html-text", forHTTPHeaderField: "Content-Type")
+            req.HTTPBody = postData
+            
+            NSURLConnection.sendAsynchronousRequest(req, queue: self.pollingQueue) {[weak self] res, data, err in
+                if err != nil {
+                    println(err)
+                    self?.pingTimer?.invalidate()
+                    if !self!.client.reconnecting {
+                        self?.client.tryReconnect(triesLeft: self!.client.reconnectAttempts)
+                    }
+                    return
+                }
+                
+                self?.doPoll()
+            }
+        }
+    }
+    
+    func sendPollMessage(msg:AnyObject) {
+        
     }
     
     // Starts the ping timer

    }
    
    func close() {
        if websocketConnected {
            self.ws?.send(PacketType.MESSAGE.rawValue + PacketType.CLOSE.rawValue)
            self.ws?.close()
        }
    }
    
    private func createURLs(params:[String: AnyObject]? = nil) -> (NSURL, NSURL) {
        var url = "\(self.client.socketURL)/socket.io/?transport="
        var urlPolling:String
        var urlWebSocket:String
        
        if self.client.secure {
            urlPolling = "https://" + url + "polling"

            }
        }
        
        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)
        
        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
                }
                
                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)
                        
                        if type != "0" {
                            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
                                }
                        }
                    }
                    
                    self?.doPoll()
                    self?.startPingTimer()
                }
        }
    }
    
