I suspect I misunderstand your protocol. Based on my reading of your sample code, the timing seems to be:
- Client sends a file to the server in response to establishing a successful connection.
- Server receives the client's file and sends a file to the client in response.
- Client receives the server's file and sends a file to the server in response.
- The last two steps repeat until some ending condition is reached.
That sort of protocol tends to be self-synchronizing. Neither the server nor the client will send a file out of turn, so logic for handling out-of-turn files isn't necessary. If it weren't for the client taking initiative, nothing would happen at all.
If you want to identify when one or the other side is desynchronized, then you can store a "sender" or "receiver" state in a variable, or in $_[HEAP]. The client begins in the "sender" state, and the server starts in "receiver" mode. If input arrives when a client or server is in "sender" mode, that's an error. Each side switches from sender to receiver upon successful transmission of a file. Each side switches from receiver to sender upon successful receipt of a file.