I haven't done much with sockets, but I seem to recall someone else having a similar problem (on a different site), and they were advised to use binary variables, rather than local/global variables to store the information.

I'll see if I can find the topic again and post more details or a link to the topic.