Socket.io: connect to and disconnect from namespaces manually

I have created somes buttons to connect and disconnect from my namespaces:

<div id="message">no message</div>
<button id="disNamespace1" type="button"> Disconnect from namespace1</button>
<button id="disNamespace2" type="button"> Disconnect from namespace2</button>
<button id="conNamespace1" type="button"> connection to namespace1</button>
<button id="conNamespace2" type="button"> connection to namespace2</button>

and I'm handling this buttons like this:

$('#conNamespace1').click(function(ev){
    mySocket = io.connect('http://localhost:1337/namespace1');
    mySocket.on("msg:from:namspace1", function(msg){
        $('#message').text(msg);
    });
});
$('#conNamespace2').click(function(ev){
    mySocket = io.connect('http://localhost:1337/namespace2');
    mySocket.on("msg:from:namspace2", function(msg){
        $('#message').text(msg);
    });
});

$('#disNamespace1').click(function(ev){
    console.log('trying disconnect');
    io.sockets['http://localhost:1337'].disconnect();
});
$('#disNamespace2').click(function(ev){
    io.sockets['http://localhost:1337'].disconnect();
});

My server:

socketNamespace1 = io.of('/namespace1');
socketNamespace2 = io.of('/namespace2');

socketNamespace1.on('connection', function(mySocket){
    console.log('namespace1: new user ');
    mySocket.emit('msg:from:namspace1', 'msg:from:namspace1');
    mySocket.on('disconnect', function(){
        console.log('disconnecting from namespace1');
    }); 
});
socketNamespace2.on('connection', function(mySocket){
    console.log('namespace2: new user ');
    mySocket.emit('msg:from:namspace2', 'msg:from:namspace2');
    mySocket.on('disconnect', function(){
        console.log('disconnecting from namespace2');
    });
});

My first connection and disconnection work great, but when clicking on the connection button for the second time, it doens't works anymores. What is the problem ?

Try this:

mySocket = io.connect('http://localhost:1337/namespace1', {'force new connection': true});

From socket.io docs:

When called, it creates a new Manager for the given URL, and attempts to reuse an existing Manager for subsequent calls, unless the multiplex option is passed with false. Passing this option is the equivalent of passing 'force new connection': true.

So maybe socket.io treats your second "socket.connect()" as subsequent call...