         }
     }
     
-    public func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
-        guard let peer = sock.peer else {
-            return
-        }
-        self.scheduleNextKeepaliveTimer(peer)
-    }
+//    public func socket(_ sock: GCDAsyncSocket, didWriteDataWithTag tag: Int) {
+//        guard let peer = sock.peer else {
+//            return
+//        }
+//        self.scheduleNextKeepaliveTimer(peer)
+//    }
     
     public func socket(_ sock: GCDAsyncSocket, didReadPartialDataOfLength partialLength: UInt, tag: Int) {
         guard let peer = sock.peer else {


    func sendDataInternal(_ peer: BPPeer, data: Data) {
        // send header first. Then separator. Then send body.
        var length: UInt = UInt(data.count)
        let senddata = NSMutableData.init(bytes: &length, length: 8)
        senddata.append(self.headerTerminator)
        senddata.append(data)
        peer.socket?.write(senddata as Data, withTimeout: Timeouts.body.rawValue, tag: DataTag.tag_WRITING.rawValue)
    }
    

        self.scheduleNextKeepaliveTimer(peer)
    }
    
//    public func socket(_ sock: GCDAsyncSocket, didWritePartialDataOfLength partialLength: UInt, tag: Int) {
//        guard let peer = sock.peer else {
//            return
//        }
//        self.scheduleNextKeepaliveTimer(peer)
//    }
    
    public func socket(_ sock: GCDAsyncSocket, shouldTimeoutReadWithTag tag: Int, elapsed: TimeInterval, bytesDone length: UInt) -> TimeInterval {
        return self.calcTimeExtension(sock, tag: tag)
    }
    
//    public func socket(_ sock: GCDAsyncSocket, shouldTimeoutWriteWithTag tag: Int, elapsed: TimeInterval, bytesDone length: UInt) -> TimeInterval {
//        return self.calcTimeExtension(sock, tag: tag)
//    }
    
    public func calcTimeExtension(_ sock: GCDAsyncSocket, tag: Int) -> TimeInterval {
        guard let peer = sock.peer else {
            return 0
        }
        
        let timeSinceLastData = abs(peer.lastReceivedData.timeIntervalSinceNow)
        if timeSinceLastData > (2.0 * Timeouts.keepAlive.rawValue) {
            // timeout!
            NSLog("BluepeerObject: socket timed out waiting for read/write - data last seen \(timeSinceLastData). Tag: \(tag). Disconnecting.")
            sock.disconnect()
            return 0
        } else {
            // extend
            NSLog("BluepeerObject: extending socket timeout by \(Timeouts.keepAlive.rawValue)s bc I saw data \(timeSinceLastData)s ago")
            return Timeouts.keepAlive.rawValue
        }
    }
}
