Node not-persistent net connection

I have a node app that connects to a server, so that my code can easily send data to it since I just leave the connection open constantly. But the problem is that the app never exits because the connection is still active, even after it's done being used.

The problem is there isn't one solid code path, so there isn't one place that I could end the connection. Is there a way to tell node to exit as soon as all the other code is finished, even though the connection is still open?

I'm going for something like fs.watchFile

process.exit(exitCode)

http://nodejs.org/api/process.html#process_process_exit_code

Aside: It really sounds like the code is due for a refactor with clear socket handling in mind. Refactor early, refactor often.