I really don't understand your problem, can't you do something like this:
var states = {
invalid_data_received: -1,
nothing_received: 0,
received_name: 1,
received_some_data: 2
},
state = states.nothing_received;
socket.on(’message’, function(msg, rinfo) {
msg = msg.toString();
if (state === states.nothing_received) {
do_something(msg);
} else if (state === states.received_name) {
do_something_else(msg);
} else if (state === states.received_some_data) {
do_even_more(msg);
} else if (state === states.invalid_data_received) {
// incorrect message received
}
});