Please properly format the stack trace, see Wiki - how to post code on this forum.
It makes it easier for others to read. You can edit your post by clicking the penicl icon in the bottom right.
As for the cause:
It seems that its not happy with your use of socket
You should define this script in the scripting library of this project. There are some weird behaviors (most notable is the need to import system
) with contexts in the event scripts.
These are almost always corrected by defining the script in the script library and calling the function from the event script.