     func runTest(_ caseNum: Int) {
         let s = createSocket("runCase",caseNum)
         self.socketArray.append(s)
-        s.onText = {(text: String) in
-            s.write(string: text)
+        s.onText = { [weak s]  (text: String) in
+            s?.write(string: text)
         }
-        s.onData = {(data: Data) in
-            s.write(data: data)
+        s.onData = { [weak s]  (data: Data) in
+            s?.write(data: data)
         }
         var once = false
-        s.onDisconnect = {[unowned self] (error: NSError?) in
+        s.onDisconnect = {[weak self, weak s] (error: NSError?) in
             if !once {
                 once = true
                 print("case:\(caseNum) finished")
-                self.verifyTest(caseNum)
-                self.removeSocket(s)
+                self?.verifyTest(caseNum)
+                self?.removeSocket(s)
             }
         }
         s.connect()

    func finishReports() {
        let s = createSocket("updateReports",0)
        self.socketArray.append(s)
        s.onDisconnect = {[unowned self] (error: NSError?) in
            print("finished all the tests!")
            self.removeSocket(s)
        }
        s.connect()
    }

    func verifyTest(_ caseNum: Int) {
        let s = createSocket("getCaseStatus",caseNum)
        self.socketArray.append(s)
        s.onText = { (text: String) in
            let data = text.data(using: String.Encoding.utf8)
            do {
                let resp: Any? = try JSONSerialization.jsonObject(with: data!,
