             peer.announced = true
         }
         
+        peer.connectBlock = {
+            do {
+                // pick the address to connect to INSIDE the block, because more addresses may have been added between announcement and inviteBlock being executed
+                guard let hhaddresses = peer.dnsService?.resolvedAddressInfo, hhaddresses.count > 0, var chosenAddress = hhaddresses.filter({ $0.isCandidateAddress(bluetoothOnly: limitToBluetooth) }).first else {
+                    DLog("BluepeerObject inviteBlock: no resolvedAddresses or candidates after invite accepted/rejected, BAILING")
+                    return
+                }
+                
+                // if we can use wifi, then use wifi if possible
+                if !self.bluetoothOnly && !chosenAddress.isWifiInterface() {
+                    let wifiCandidates = hhaddresses.filter({ $0.isCandidateAddress(bluetoothOnly: self.bluetoothOnly) && $0.isWifiInterface() })
+                    if wifiCandidates.count > 0 {
+                        chosenAddress = wifiCandidates.first!
+                    }
+                }
+                
+                DLog("BluepeerObject inviteBlock: peer has \(hhaddresses.count) addresses, chose \(chosenAddress.addressAndPortString) on interface \(chosenAddress.interfaceName)")
+                let sockdata = chosenAddress.socketAsData()
+                
+                // TODO: when migrating Wifibooth, blueprint: make sure browserFoundPeer calls stopBrowsing()!
+                // 1.1: don't stop browsing automatically, delegate must do that now
+                //                    self.stopBrowsing() // stop browsing once user has done something. Destroys all HHService except this one!
+                
+                if let oldSocket = peer.socket {
+                    DLog("**** BluepeerObject: PEER ALREADY HAD SOCKET, DESTROYING...")
+                    oldSocket.synchronouslySetDelegate(nil)
+                    oldSocket.disconnect()
+                    peer.socket = nil
+                }
+                
+                peer.socket = GCDAsyncSocket.init(delegate: self, delegateQueue: self.socketQueue)
+                peer.socket?.isIPv4PreferredOverIPv6 = false
+                peer.state = .connecting
+                try peer.socket?.connect(toAddress: sockdata, viaInterface: chosenAddress.interfaceName, withTimeout: 20.0)
+            } catch {
+                DLog("BluepeerObject: could not connect.")
+            }
+        }
+        
         self.dispatch_on_delegate_queue({
-            delegate.browserFoundPeer?(role, peer: peer, customData: (customData.isEmpty ? nil : customData), inviteBlock: { (timeoutForInvite) in
-                do {
-                    // pick the address to connect to INSIDE the block, because more addresses may have been added between announcement and inviteBlock being executed
-                    guard let hhaddresses = peer.dnsService?.resolvedAddressInfo, hhaddresses.count > 0, var chosenAddress = hhaddresses.filter({ $0.isCandidateAddress(bluetoothOnly: limitToBluetooth) }).first else {
-                        DLog("BluepeerObject inviteBlock: no resolvedAddresses or candidates after invite accepted/rejected, BAILING")
-                        return
-                    }
-                    
-                    // if we can use wifi, then use wifi if possible
-                    if !self.bluetoothOnly && !chosenAddress.isWifiInterface() {
-                        let wifiCandidates = hhaddresses.filter({ $0.isCandidateAddress(bluetoothOnly: self.bluetoothOnly) && $0.isWifiInterface() })
-                        if wifiCandidates.count > 0 {
-                            chosenAddress = wifiCandidates.first!
-                        }
-                    }
-                    
-                    DLog("BluepeerObject inviteBlock: peer has \(hhaddresses.count) addresses, chose \(chosenAddress.addressAndPortString) on interface \(chosenAddress.interfaceName)")
-                    let sockdata = chosenAddress.socketAsData()
-
-                    // TODO: when migrating Wifibooth, blueprint: make sure browserFoundPeer calls stopBrowsing()!
-                    // 1.1: don't stop browsing automatically, delegate must do that now
-//                    self.stopBrowsing() // stop browsing once user has done something. Destroys all HHService except this one!
-                    
-                    if let oldSocket = peer.socket {
-                        DLog("**** BluepeerObject: PEER ALREADY HAD SOCKET, DESTROYING...")
-                        oldSocket.synchronouslySetDelegate(nil)
-                        oldSocket.disconnect()
-                        peer.socket = nil
-                    }
-                    
-                    peer.socket = GCDAsyncSocket.init(delegate: self, delegateQueue: self.socketQueue)
-                    peer.socket?.isIPv4PreferredOverIPv6 = false
-                    peer.state = .connecting
-                    try peer.socket?.connect(toAddress: sockdata, viaInterface: chosenAddress.interfaceName, withTimeout: timeoutForInvite)
-                } catch {
-                    DLog("BluepeerObject: could not connect.")
-                }
-            })
+            delegate.browserFoundPeer?(role, peer: peer, customData: (customData.isEmpty ? nil : customData))
         })
     }
     

            newPeer.role = .client
            newPeer.socket = newSocket
            self.peers.append(newPeer) // always add as a new peer, even if it already exists. This might result in a dupe if we are browsing and advertising for same service. The original will get removed on receiving the name of other device, if it matches
            DLog("BluepeerObject: accepting new connection from \(connectedHost). Peers(n=\(self.peers.count)) after adding: \(self.peers)")
            
            // CONVENTION: CLIENT sends SERVER 32 bytes of its name -- UTF-8 string
            self.logDelegate?.didReadWrite("readName")

            }
        }
        
        print("BP " + formatter.string(from: Date.init()) + " - " + str)
    #endif
}

