     }
 }
 
-private struct binaryMessage {
-    let messageFormat = "45%@-[\"%@\",%@]"
-    var event:String!
-    var data:NSData!
-    
-    init(event:String, data:NSData) {
-        self.event = event
-        self.data = data
-    }
-    
-    func createMessage() -> String {
-        return NSString(format: messageFormat, event)
-    }
-}
-
-private struct SocketMessage {
+private struct Event {
     var event:String!
     var args:Any!
     var placeholders:Int!

    private var handlers = [EventHandler]()
    var io:SRWebSocket?
    var pingTimer:NSTimer!
    private var lastSocketMessage:SocketMessage?
    var secure = false
    
    init(socketURL:String, secure:Bool = false) {

        self.io?.open()
    }
    
    // Creates a binary message, ready for sending
    private func createBinaryDataForSend(data:NSData) -> NSData {
        var byteArray = [UInt8](count: 1, repeatedValue: 0x0)
        byteArray[0] = 4
        var mutData = NSMutableData(bytes: &byteArray, length: 1)
        mutData.appendData(data)
        return mutData
    }
    
    // Sends a message
    func emit(event:String, args:Any? = nil) {
        if (!self.connected) {
            return
        }
        var frame:Event!
        var str:String!
        
        if let dict = args as? NSDictionary {
            // Check for binary data
            let (newDict, hadBinary, binaryDatas) = self.parseNSDictionary(event: event, dict: dict)
            if (hadBinary) {
                frame = Event(event: event, args: newDict, placeholders: binaryDatas!.count)
                str = frame.createBinaryMessage()
                self.io?.send(str)
                for data in binaryDatas! {
                    let sendData = self.createBinaryDataForSend(data)
                    self.io?.send(sendData)
                }
                return
            }
        } else if let binaryData = args as? NSData {
            // args is just binary
            frame = Event(event: event, args: ["_placeholder": true, "num": 0], placeholders: 1)
            str = frame.createBinaryMessage()
            self.io?.send(str)
            let sendData = self.createBinaryDataForSend(binaryData)
            self.io?.send(sendData)
            return
        }
        
        frame = Event(event: event, args: args)
        str = frame.createMessage()
        
        // println("Sending: \(str)")
        self.io?.send(str)
