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

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

        // loop through peers, disconenct all sockets
        for peer in self.peers {
            NSLog("BluepeerObject disconnectSession: disconnecting \(peer.displayName)")
            peer.socket?.synchronouslySetDelegate(nil) // we don't want to run our disconnection logic below
            peer.socket?.disconnect()
            peer.socket = nil
            peer.dnsService?.endResolve()
