Just thought you would want to know that there's an off-by-one error in SendText that causes it to send a null at the end of every message. I figured this out after troubleshooting a handshake with Wireshark.

Clients aren't being closed when the OnTCPAccpet function is over, and I can't initiate new connections if I manually call newTcp.__Delete() at the end of the function

Edit: Problem solved. On line 163 a.remove(obj.socket) should be a.remove(obj.socket, ""), to avoid messing up every other socket ID by decrementing them by 1. But still, seeing as the contents of a[obj.socket] is obj, the socket never gets automatically deleted at the end of the function by the garbage collector

Also, maybe I am missing something, but I am having trouble implementing the behavior that I want.

What I would like to do is to have a "Master" issue a "Slave" an instruction, then once the instruction has been carried out (Could be ages later - hours even), the Slave Reopens a socket back to the master to report job done.

So the message flow would look like:

MASTER: DoSomething

SLAVE: ACK

[Hours pass]

SLAVE: DidSomething

MASTER: ACK

Also, i would like this to be asynchronous, so the master could ask the slave to do a number of things, and as each is completed, it connects back to the master.

I knocked up some test code, but it does not seem to work properly.

On the slave, I set up an asynch thread (SetTimer -2000) to simulate a reply back later, but in reality I would like to call the func immediately (SetTimer -0) and have a Sleep in the func to simulate work taking some time to do.

However, if I do that, asynch calls only work once - you cannot queue up more than one DoSomething request, as the ACK does not seem to come back.

Any ideas?

Run both scripts on local machine, hit F12 to have the master send a "DoSomething" request to the slave.

Set this.mode := 1 in the Slave to enable to logic that I wish to use. Notice how in this mode, if you spam F12 quickly, the messages stop flowing.