write ECONNRESET error and socket connection checking in node.js

I get this error occasionally when running my node.js script.

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: write ECONNRESET
    at errnoException (net.js:768:11)
    at Object.afterWrite (net.js:592:19)

What causes this error? I read someplace that this is caused due to attempting to write data to a closed socket. Is that right?

If that is the case, How do I check if a socket connection is active?
I found this SO question here, but no one had answered there.

And thirdly, is a simple try catch around the socket.write statement, enough to handle this error? Or does it emit error events which I must handle?

Read more here: Source link