 
         delegate?.connectionDidClose(error: error)
     }
+
+    public func connectionDidOpen(connection: SocketConnection!) {
+        connectionStarted()
+    }
+
+    public func connectionDidFailToOpen(error: Error) {
+        delegate?.connectionDidFailToOpen(error: error)
+    }
+
+    public func connectionDidReceiveData(connection: SocketConnection!, data: Data) {
+        hubConnectionDidReceiveData(data: data)
+    }
+
+    public func connectionDidClose(error: Error?) {
+        hubConnectionDidClose(error: error)
+    }
 }
-
-fileprivate class HubSocketConnectionDelegate : SocketConnectionDelegate {
-    private weak var hubConnection: HubConnection?
-
-    fileprivate init(hubConnection: HubConnection!) {
-        self.hubConnection = hubConnection
-    }
-
-    public func connectionDidOpen(connection: SocketConnection!) {
-        hubConnection?.connectionStarted()
-    }
-
-    public func connectionDidFailToOpen(error: Error) {
-        hubConnection?.delegate?.connectionDidFailToOpen(error: error)
-    }
-
-    public func connectionDidReceiveData(connection: SocketConnection!, data: Data) {
-        hubConnection?.hubConnectionDidReceiveData(data: data)
-    }
-
-    public func connectionDidClose(error: Error?) {
-        hubConnection?.hubConnectionDidClose(error: error)
-    }
-}


import Foundation

public class HubConnection {

    private var invocationId: Int = 0
    private let hubConnectionQueue: DispatchQueue
    private var socketConnectionDelegate: HubSocketConnectionDelegate?
    private var pendingCalls = [String: ServerInvocationHandler]()
    private var callbacks = [String: ([Any?], TypeConverter) -> Void]()
    private var handshakeHandled = false

        self.connection = connection
        self.hubProtocol = hubProtocol
        self.hubConnectionQueue = DispatchQueue(label: "SignalR.hubconnection.queue")
        self.connection.delegate = self
    }

    public func start(transport: Transport? = nil) {
