                 // If currently a guest, stop looking for host
                 stopSearchingForHosts()
                 
-                // Pass to delegate
-                delegate?.connectedToPeer(peerID)
+                if self.host == peerID {
+                    if session.connectedPeers.count >= maxNumPeerInRoom {
+                        disconnect()
+                        return
+                    } else {
+                        // Pass to delegate
+                        delegate?.connectedToPeer(peerID)
+                    }
+                }
             } else {
                 DLog("%@", "not connected to \(session.hashValue)")
-                // If self is disconnected or current host is disconnected
+                
+                // If self is disconnected from current host
                 if self.host == peerID {
-                    DLog("%@", "Host was removed")
+                    DLog("%@", "disconnected from host")
                     session.disconnect()
-                    delegate?.disconnectedFromSession()
+                    delegate?.disconnectedFromSession(peerID)
+                    return
                 }
             }
-            
-            delegate?.connectedPeersInSessionChanged(session.connectedPeers)
+            // If self did not disconnect deliberately
+            if self.host != nil {
+                delegate?.connectedPeersInSessionChanged(session.connectedPeers)
+            }
+        } else {
+            delegate?.connectingToPeer(peerID)
         }
     }
     

    public func getMyPeerID() -> MCPeerID {
        return myPeerId
    }
    // Debug mode
    private func DLog(message: String, _ function: String) {
        if debugMode {

    public var debugMode = false
    public var autoAcceptGuests = true
    
    static let defaultTimeout: NSTimeInterval = 7
    private let maxNumPeerInRoom = 4
    private var serviceAdvertiser: MCNearbyServiceAdvertiser?
    private var serviceBrowser: MCNearbyServiceBrowser?
    private var foundHosts = [MCPeerID]()
