     
     /** Called any time the IceConnectionState changes. */
     public func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState){
-        if newState == .failed {
+        switch newState {
+        case .failed:
             sendPeerMessage(messageType: "connectivityError", payload: nil)
-        }
-        print("RTCPeer:RTCPeerConnectionDelegate: peer connection ice state did change")
+            print("ERROR: RTCPeer:RTCPeerConnectionDelegate: peer ice failed")
+        case .connected:
+            sendPeerMessage(messageType: "endOfCandidates", payload: nil)
+            print("RTCPeer:RTCPeerConnectionDelegate: peer ice state connected")
+        case .completed:
+            print("RTCPeer:RTCPeerConnectionDelegate: peer ice state completed")
+        default:
+            print("RTCPeer:RTCPeerConnectionDelegate: peer connection ice state did change")
+        }
+        
+        
     }
     
     

    
    /** New ice candidate has been found. */
    public func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate){
        print(candidate.sdp)
        let candidate:[String: Any] = ["candidate":candidate.sdp,"sdpMid":candidate.sdpMid!,"sdpMLineIndex":candidate.sdpMLineIndex]
        sendPeerMessage(messageType: "candidate", payload: ["candidate":candidate])
        print("RTCPeer:RTCPeerConnectionDelegate: peer connection did generate new ICE candidate")

    fileprivate func sendMessage(event: String, data: Any){
        
        let msg = ["event": event, "data": data]
        print("SENDING:")
        print(msg)
        sendMessage(msg)
    }
    
