     public func serviceDidPublish(_ servicePublisher: HHServicePublisher) {
         // create serverSocket
         if let socket = self.serverSocket {
-            socket.delegate = nil
+            socket.synchronouslySetDelegate(nil)
             socket.disconnect()
             self.serverSocket = nil
         }

    public func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
        guard let peerIndex = self.peers.index(where: {sock == $0.socket}) else {
            NSLog("BluepeerObject socketDidDisconnect: WARNING, expected to find a peer, calling peerConnectionAttemptFailed")
            sock.delegate = nil
            self.dispatch_on_delegate_queue({
                self.sessionDelegate?.peerConnectionAttemptFailed?(.unknown, peer: nil, isAuthRejection: false)
            })

        peer.keepaliveTimer?.invalidate()
        peer.keepaliveTimer = nil
        peer.socket = nil
        sock.synchronouslySetDelegate(nil)
        switch oldState {
        case .connected:
            self.peers.remove(at: peerIndex)
