Add argument: device class.
Close sockets in case of error, and keep listening on new connections.
Code cleaning.