         }
 
         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)
         }
 

import Foundation

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


        }

        webSocket!.event.close = { code, reason, clean in
            // the transport could have already been closed as a result of an error. In this case we should not call
            // transportDidClose again on the delegate.
            guard !self.markTransportClosed() else {
                // TODO: add logging
                return
            }

            if clean {
                self.delegate?.transportDidClose(nil)
            } else {
