Prevent exit when connection attempt fails.

Check that the "error" event has any registered listeners before emitting it. This prevents the process from dying as that would be the default behaviour in node.