module.exports = function(app, io, authentication) {
  
  var docker = require('docker.js')(app, io);

  // New Authenticated Client
  function connection(socket, userdata) {
    console.log("=> new connection from "+userdata.username);
    docker.connect(socket);
  }

  // Handle Connection Authentication
  require('socketio-auth')(io, {
    authenticate: function (socket, data, callback) {
      console.log("=> authenticate: ", data.username);
      authentication(data.username, data.password,
                     function() {
                       console.log("####LOGIN-SUCESS####");
                       callback(null, true)
                     },
                     function() {
                       console.log("####LOGIN-FAIL####");
                       callback(new Error("wrong credentials"))
                     });
    },
    postAuthenticate: connection,
    timeout: "none"
  });

  var module = {};
  return module;
}
