             request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
         }
     }
+
+    private func markTransportClosed() -> Bool {
+        var previousCloseStatus = false
+
+        transportQueue.sync {
+            previousCloseStatus = isTransportClosed
+            isTransportClosed = true
+        }
+
+        return previousCloseStatus
+    }
 }
 
 fileprivate enum WebSocketsTransportError: Error {

import Foundation

public class WebsocketsTransport: Transport {
    var webSocket:WebSocket? = nil
    public weak var delegate: TransportDelegate? = nil


        }

        webSocket!.event.error = { error in
            // This handler should not be called after the close event but we need to mark the transport as closed to prevent calling transportDidClose
            // on the delegate multiple times so we can as well add the check and log
            guard !self.markTransportClosed() else {
                // TODO: add logging
                return
            }

            self.delegate?.transportDidClose(error)
        }

