     var publisher: HHServicePublisher?
     var browser: HHServiceBrowser?
     var bluetoothOnly: Bool = false
-    open var advertising: Bool = false
+    open var advertising: RoleType?
     open var browsing: Bool = false
+    var onLastBackground: (advertising: RoleType?, browsing: Bool) = (nil, false)
     open var serviceType: String
     var serverPort: UInt16 = 0
     var versionString: String = "unknown"

    open var bluetoothState : BluetoothState = .unknown
    var bluetoothPeripheralManager: CBPeripheralManager
    open var bluetoothBlock: ((_ bluetoothState: BluetoothState) -> Void)?
    open var disconnectOnWillResignActive: Bool = false {
        didSet {
            if disconnectOnWillResignActive {
                NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
            } else {
                NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
            }
        }
    }
    let headerTerminator: Data = "\r\n\r\n".data(using: String.Encoding.utf8)! // same as HTTP. But header content here is just a number, representing the byte count of the incoming nsdata.
    let keepAliveHeader: Data = "0 ! 0 ! 0 ! 0 ! 0 ! 0 ! 0 ! ".data(using: String.Encoding.utf8)! // A special header kept to avoid timeouts
    let socketQueue = DispatchQueue(label: "xaphod.bluepeer.socketQueue", attributes: [])

            peer.dnsService = nil
            peer.state = .notConnected
        }
        self.killAllKeepaliveTimers()
        self.peers = [] // remove all peers!
    }
    
    open func connectedRoleCount(_ role: RoleType) -> Int {
