The socket is immediately closing when you attempt to connect, causing you to try to connect again. Add an interval between tries:
socket.onclose = function() {
state.connected = false;
setInterval(function() {
connect(settings);
}, 1000);
}