Socket.io connecting/disconnecting strange behaviour

I'm experimenting with node.js / socket.io and came across some strange things when connecting and disconnecting to the socket.io server.

Socket.io version 1.0.6 Node.js version 0.10.29 Express version 4.7.2

I'm storing my connections to a JavaScript object/array that I declared at the top of app.js, I want to add user objects to it later. Every time I connect I add something to this array and every time I disconnect I remove it. The server code:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

var connections = {}; // in here I want to store my connections

app.set('view engine', 'ejs');


app.get('/', function(req, res) {
    res.render(__dirname + '/views/index.ejs');
});

app.get('/something', function(req, res) {

    res.render(__dirname + '/views/something.ejs');

    // socket.io connetion
    io.on('connection', function(socket) {        
        // HERE is the PROBLEM
        connections[socket.id] = "user object will come here"; // add user object to connections
        console.log('======== connection added ========'); 
        console.log(connections);

        socket.on('disconnect', function() {
            delete connections[socket.id]; // remove from connections
            console.log('======== connection removed ========');
            console.log(connections);
        });
    });

});

server.listen(3000);

The problem however, every time make a new connection it will do the code after the “HERE is the PROBLEM comment” but than multiplied by the connections I started, even if they are closed/disconnected.

So for exmple:

I start a connection by typing localhost:3000/something in my browser. It will then print the following to the terminal (I mean execute the code):

...
console.log('======== connection added ========'); 
console.log(connections);

That looks OK, it will show one connection when console.log(connections)

But then when I start another connection it will do:

...
console.log('======== connection added ========'); 
console.log(connections);

...
console.log('======== connection added ========'); 
console.log(connections);

The connections object will show the right amount of connections, that is not an issue. But it printed it out twice to the console! WHY?!

Then when I close a browser or browser tab, I disconnect and the following code will execute:

delete connections[socket.id]; // remove from connections
console.log('======== connection removed ========');
console.log(connections);

delete connections[socket.id]; // remove from connections
console.log('======== connection removed ========');
console.log(connections);

The connections object will show 1 connection, which is correct but it is printed out twice. If I would start a new connection, there would be 2 connections and it will print out “connection added + connections” 3 times, it should only print it out once. So if I would start 10 connections, close 9 connections and start a new connections, I will have 2 connections going on and it will print “connection added + connections” 11 times...

So the

io.on('connection', function(socket) {});

will run amount of connections started (even if they are long closed) for every connection that starts. Sorry for my poor English and long description but wouldn't now how to 'name' this problem.

Moving the socket.io code out of

app.get('/something', function(req, res) {//code here});

and placing it after server.listen(3000) seemed to have solved this problem. However I wanted to have a socket.io connection only in a certain page. How would I do this?