NodeJS is a non-blocking async platform.
In your case,
netSocket.write(messages);
is an async method; therefore, netSocket.end() is called before write is complete.
The correct use would be:
netSocket.write(messages, function(err) { netSocket.end(); });
The second argument here is a callback function that will be called once the ‘write’ method finishes its job.
I would recommend you read/watch more about NodeJS, async styles and callbacks.
Here is a great place to start: https://www.youtube.com/watch?v=GJmFG4ffJZU
And of course, the NodeJS API docs regarding net sockets.