         if rd.streamError != nil || wr.streamError != nil {
             return true
         }
-        if rd.hasBytesAvailable || frames.count > 0 || inputBytesLength > 0 || outputBytesLength > 0 {
+        if rd.hasBytesAvailable || frames.count > 0 || inputBytesLength > 0 {
+            return true
+        }
+        if outputBytesLength > 0 && wr.hasSpaceAvailable{
             return true
         }
         return false


private let manager = Manager()

public class WebSocket: Hashable {
    private var ws : InnerWebSocket
    private var id = manager.nextId()
    private var opened : Bool
    public var hashValue: Int { return id }
    public convenience init(_ url: String){
        self.init(request: NSURLRequest(URL: NSURL(string: url)!), subProtocols: [])
    }
    public convenience init(_ url: String, subProtocols : [String]){
        self.init(request: NSURLRequest(URL: NSURL(string: url)!), subProtocols: subProtocols)
    }
    public convenience init(_ url: String, subProtocol : String){
        self.init(request: NSURLRequest(URL: NSURL(string: url)!), subProtocols: [subProtocol])
    }
    public init(request: NSURLRequest, subProtocols : [String] = []){
        opened = true
        ws = InnerWebSocket(request: request, subProtocols: subProtocols, stub: false)
    }
    public init(){
        opened = false
        ws = InnerWebSocket(request: NSURLRequest(), subProtocols: [], stub: true)
    }
    public var url : String{ return ws.url }
    public var subProtocol : String{ return ws.subProtocol }
    public var compression : WebSocketCompression{
        get { return ws.compression }
        set { ws.compression = newValue }
    }
    public var allowSelfSignedSSL : Bool{
        get { return ws.allowSelfSignedSSL }
        set { ws.allowSelfSignedSSL = newValue }
    }
    public var services : WebSocketService{
        get { return ws.services }
        set { ws.services = newValue }
    }
    public var event : WebSocketEvents{
        get { return ws.event }
        set { ws.event = newValue }
    }
    public var eventQueue : dispatch_queue_t?{
        get { return ws.eventQueue }
        set { ws.eventQueue = newValue }
    }
    public var binaryType : WebSocketBinaryType{
        get { return ws.binaryType }
        set { ws.binaryType = newValue }
    }
    public var readyState : WebSocketReadyState{
        return ws.readyState
    }
    public func open(request: NSURLRequest, subProtocols : [String] = []){
        if opened{
            return

        opened = false
        ws.close(code, reason: reason)
    }
    /**
    Transmits message to the server over the WebSocket connection.
    
    :param: message The data to be sent to the server.
    */
    public func send(message : Any){
        if !opened{
            return
        }
        ws.send(message)
    }
    /**
    Transmits a ping to the server over the WebSocket connection.
     
    :param: optional message The data to be sent to the server.
    */
    public func ping(message : Any){
        if !opened{
            return
        }
        ws.ping(message)
    }
    /**
    Transmits a ping to the server over the WebSocket connection.
    */
    public func ping(){
        if !opened{
            return
