I guess that would basically need more than one thread. In this version the client sends the packet payload as soon as the handshake/crypto succeeds. Therefore it has to call the other functions first, read/write to socket, then take the packet data from the instance var. I think this solution is sufficient for this case. Or you have to explain me how to change the while loop in this method so that this method is returning and I can send the packet to the tinc class as soon as the handshake is done.