     }
 
     // MARK: MCSessionDelegate protocol conformance
-    
-    func session(session: MCSession, didReceiveCertificate certificate: [AnyObject]?, fromPeer peerID: MCPeerID, certificateHandler: (Bool) -> Void) {
-        certificateHandler(true)
-    }
 
     func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState) {
-        NSLog("Peer [%@] changed state to %@", peerID.displayName, MCSession.stringForPeerConnectionState(state))
-        
+        NSLog("%@ [%@] %@", __FUNCTION__, peerID.displayName, MCSession.stringForPeerConnectionState(state))
+
         switch state {
         case .Connecting:
             connectingPeersOrderedSet.addObject(peerID)

    }
    
    func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
        // Decode the incoming data to a UTF8 encoded string
        let receivedMessage = NSString(data: data, encoding: NSUTF8StringEncoding)
        
        NSLog("didReceiveData %@ from %@", receivedMessage!, peerID.displayName)
    }
    
    func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) {
        NSLog("didStartReceivingResourceWithName [%@] from %@ with progress [%@]", resourceName, peerID.displayName, progress)
    }
    
    func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) {
        NSLog("didFinishReceivingResourceWithName [%@] from %@", resourceName, peerID.displayName)
        
        // If error is not nil something went wrong
        if (error != nil) {
            NSLog("Error [%@] receiving resource from %@ ", error!, peerID.displayName)
        }
        else {
            // No error so this is a completed transfer. The resources is located in a temporary location and should be copied to a permenant location immediately.
            // Write to documents directory
            let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
            let copyPath = NSString(format: "%@/%@", paths.first!, resourceName)
            
            do {
                try NSFileManager.defaultManager().copyItemAtPath(localURL.path!, toPath: copyPath as String)
                // Get a URL for the path we just copied the resource to
                let url = NSURL(fileURLWithPath: copyPath as String)
                NSLog("url=%@", url)
            }
            catch _ {
                print(error?.localizedDescription)
            }
        }
    }

    // Streaming API not utilized in this sample code
    func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) {
        NSLog("didReceiveStream %@ from %@", streamName, peerID.displayName)
    }

    // MARK: MCNearbyServiceBrowserDelegate protocol conformance

    func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
        let remotePeerName = peerID.displayName
        
        if let myPeerID = session?.myPeerID {
        
            let shouldInvite = (myPeerID.displayName.compare(remotePeerName) == .OrderedDescending)
            
            if (shouldInvite) {
                NSLog("%@ Inviting [%@]", __FUNCTION__, remotePeerName)
                browser.invitePeer(peerID, toSession: session!, withContext: nil, timeout: 30.0)
            }
            else {
                NSLog("%@ Not inviting [%@]", __FUNCTION__, remotePeerName)
            }
            
            delegate?.sessionDidChangeState()
        }
    }
    
    func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
        connectingPeersOrderedSet.removeObject(peerID)
        disconnectedPeersOrderedSet.addObject(peerID)
        
        delegate?.sessionDidChangeState()
        NSLog("%@ lostPeer [%@]", __FUNCTION__, peerID.displayName)
    }

    func browser(browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: NSError) {
        NSLog("%@ %@", __FUNCTION__, error)
    }

    // MARK: MCNearbyServiceAdvertiserDelegate protocol conformance

    func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession) -> Void) {
        NSLog("%@ Accepting invitation from [%@]", __FUNCTION__, peerID.displayName)
        
        invitationHandler(true, session!)
    }

    func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError) {
        NSLog("%@ %@", __FUNCTION__, error)
    }

}
