         }
         if service.type == self.serviceType {
             service.delegate = self
+            self.servicesBeingResolved.insert(service)
             let prots = UInt32(kDNSServiceProtocol_IPv4 + kDNSServiceProtocol_IPv6)
             if (self.overBluetoothOnly) {
                 service.beginResolve(kDNSServiceInterfaceIndexP2PSwift, includeP2P: true, addressLookupProtocols: prots)

            guard let browser = self.browser else {
                NSLog("BluepeerObject: browser is MIA while browsing set true! ERROR. Setting browsing=false")
                self.browsing = false
                return
            }
            browser.endBrowse()

            return
        }
        
        NSLog("BluepeerObject: serviceDidResolve name: \(service.name), role: \(roleStr)")
        
        guard let delegate = self.sessionDelegate else {
            NSLog("BluepeerObject: WARNING, ignoring resolved service because sessionDelegate is not set")
            return
        }
        
        guard let hhaddresses: [HHAddressInfo] = service.resolvedAddressInfo, hhaddresses.count > 0 else {
            NSLog("BluepeerObject: serviceDidResolve IGNORING service because could not get resolvedAddressInfo")
            return
        }
        
        // if this peer is in the set, then add this as another address(es), otherwise add now
        var peer = self.peers.filter({ $0.displayName == service.name }).first
        var isNewPeer = false
        if peer == nil {
            peer = BPPeer.init()
            peer!.displayName = service.name
            self.peers.insert(peer!)
            isNewPeer = true
        }
        peer!.role = role
        peer!.resolvedAddressInfos.append(contentsOf: hhaddresses)
        NSLog("... this peer now has \(peer!.resolvedAddressInfos.count) addresses")

        if isNewPeer {
            self.dispatch_on_delegate_queue({
                delegate.browserFoundPeer?(role, peer: peer!, inviteBlock: { (connect, timeoutForInvite) in
                    self.stopBrowsing() // stop browsing once user has done something
                    if connect {
                        do {
                            NSLog("... user said to connect. Addresses to check: \(peer!.resolvedAddressInfos.count)")

                            // strategy: for this peer, there are potentially multiple interfaces, with each having potentially multiple IPs.
                            // prefer IPv6, then wifi; if bluetoothOnly, must not be wifi (en0)
                            
                            var wifiIP4: [(HHAddressInfo, String)] = []
                            var wifiIP6: [(HHAddressInfo, String)] = []
                            var notWifiIP4: [(HHAddressInfo, String)] = []
                            var notWifiIP6: [(HHAddressInfo, String)] = []
                            
                            for address in peer!.resolvedAddressInfos {
                                let ipAndPort = address.addressAndPortString
                                guard let lastColonIndex = ipAndPort.range(of: ":", options: .backwards)?.lowerBound else {
                                    NSLog("ERROR: unexpected return of IP address with port")
                                    assert(false, "error")
                                    return
                                }
                                let ip = ipAndPort.substring(to: lastColonIndex)
                                if ipAndPort.components(separatedBy: ":").count-1 > 1 {
                                    // ipv6 - looks like [00:22:22.....]:port
                                    let ipstr = ip.substring(with: Range(uncheckedBounds: (lower: ip.index(ip.startIndex, offsetBy: 1), upper: ip.index(ip.endIndex, offsetBy: -1))))
                                    if address.interfaceName == "en0" {
                                        wifiIP6.append((address, ipstr))
                                    } else {
                                        notWifiIP6.append((address, ipstr))
                                    }
                                } else {
                                    // ipv4 - looks like 10.1.2.3:port
                                    if address.interfaceName == "en0" {
                                        wifiIP4.append((address, ip))
                                    } else {
                                        notWifiIP4.append((address, ip))
                                    }
                                }
                            }
                            
                            NSLog("... checked. wifiIP4:\(wifiIP4.count), wifiIP6:\(wifiIP6.count), notWifiIP4:\(notWifiIP4.count), notWifiIP6:\(notWifiIP6.count)")
                            var toUse: (HHAddressInfo, String)
                            if (self.overBluetoothOnly) {
                                if (notWifiIP6.count > 0) {
                                    toUse = notWifiIP6.first!
                                } else if (notWifiIP4.count > 0) {
                                    toUse = notWifiIP4.first!
                                } else {
                                    NSLog("WARNING: could not find bluetooth address, using something else!")
                                    if (wifiIP6.count > 0) {
                                        toUse = wifiIP6.first!
                                    } else if (wifiIP4.count > 0) {
                                        toUse = wifiIP4.first!
                                    } else {
                                        peer!.state = .notConnected
                                        assert(false)
                                        return
                                    }
                                }
                            } else {
                                if (wifiIP6.count > 0) {
                                    toUse = wifiIP6.first!
                                } else if (notWifiIP6.count > 0) {
                                    toUse = notWifiIP6.first!
                                } else if (wifiIP4.count > 0) {
                                    toUse = wifiIP4.first!
                                } else if (notWifiIP4.count > 0) {
                                    toUse = notWifiIP4.first!
                                } else {
                                    peer!.state = .notConnected
                                    assert(false)
                                    return
                                }
                            }
                            NSLog("Picked IP address: \(toUse.1)")
                            peer!.socket = GCDAsyncSocket.init(delegate: self, delegateQueue: self.socketQueue)
                            peer!.socket!.isIPv4PreferredOverIPv6 = false
                            peer!.state = .connecting
                            var socketAddrData = Data.init(bytes: toUse.0.address, count: MemoryLayout<sockaddr>.size)
                            var storage = sockaddr_storage()
                            (socketAddrData as NSData).getBytes(&storage, length: MemoryLayout<sockaddr_storage>.size)
                            if Int32(storage.ss_family) == AF_INET6 {
                                socketAddrData = Data.init(bytes: toUse.0.address, count: MemoryLayout<sockaddr_in6>.size)
                            }
                            
                            try peer!.socket?.connect(toAddress: socketAddrData, viaInterface: toUse.0.interfaceName, withTimeout: timeoutForInvite)
                        } catch {
                            NSLog("BluepeerObject: could not connect.")
                        }
                    } else {
                        NSLog("... got told NOT to connect.")
                        peer!.state = .notConnected
                    }
                })
            })
        }
    }
    
    public func serviceDidNotResolve(_ service: HHService) {
