     }
   }
   
+  func clientDisconnected() {
+    // clean up if we were capturing video and had a browser loaded
+    stopVideoTimer()
+    if videoCapture != nil {
+      videoCapture!.stopRecording() {
+        self.videoCapture = nil
+        self.deleteVideo()
+      }
+    }
+    closeWebView()
+    hasOrange = false
+    isActive = false
+    title = "iWptBrowser"
+  }
+  
   /*************************************************************************************
                                   browser operations
    *************************************************************************************/

    } while cont
    socket.close()
    self.log("\(id): Socket closed")
  }
  
  func receivedRawData(id:Int, str:String) {

   *************************************************************************************/
  func startVideo(id:String) {
    if videoCapture == nil {
      stopVideoTimer()
      deleteVideo()
      videoCapture = ASScreenRecorder()
      videoCapture!.videoURL = videoUrl
      videoCapture!.bitrate = 8000000
      videoCapture!.scale = 1.0
      videoCapture!.startRecording()
      // Limit video recording to 30 minutes to make sure we don't chew up ALL of the storage
      videoTimer = Timer.scheduledTimer(timeInterval: 1800, target: self, selector: #selector(self.videoTimerFired), userInfo: nil, repeats: false)
      sendMessage(id:id, message:"OK")
    } else {
      sendMessage(id:id, message:"ERROR")
    }
  }
  
  @objc func videoTimerFired() {
    self.log("Video timer fired, stopping recording")
    stopVideoTimer()
    if videoCapture != nil {
      videoCapture!.stopRecording() {
        self.videoCapture = nil
      }
    }
  }
  
  func stopVideoTimer() {
    if videoTimer != nil {
      if videoTimer!.isValid {
        videoTimer!.invalidate()
      }
      videoTimer = nil
    }
  }
  
  func stopVideo(id:String) {
    stopVideoTimer()
    if videoCapture != nil {
      videoCapture!.stopRecording() {
        self.videoCapture = nil
