Starting with 3.0, express applications have become request handler
functions that you pass to http or httpServer instances. You need
to pass the Server to socket.io, and not the express application
function.

Registers a middleware, which is a function that gets executed for
every incoming Socket and receives as parameter the socket and a
function to optionally defer execution to the next registered
middleware.

var io =require('socket.io')();

io.use(function(socket, next){

if(socket.request.headers.cookie)return next();

next(newError('Authentication error'));

});

Errors passed to middleware callbacks are sent as special error
packets to clients.