How to know my socket connect is success or not?

I want to do something when the nodejs server is started or I will do something else not related to the server.

But how could I know that. There is no property that shows the status of the connection, and because the socket is asynchronous that I can't get the err by try..catch...

The following is my clinet code.

  var socket = io.connect('http://localhost:3000/monitor');
  socket.on('message', function (data) {
                 console.log(data);
             });

Anyone can tell me how to if(is connected).. else.. in this place?

Try listening to the following events:

socket.on( 'connect', function() {

});

socket.on( 'disconnect', function() {

});

socket.on( 'connect_failed', function() {

});

socket.on( 'error', function() {

});

Socket.io has some bugs in firing some events so it's better to listen to all of those that might mean a connection failed.