var toggleConnectionStatus = function(display) {
    var conn_status = document.getElementById('conn-status');
    conn_status.innerHTML = display;
};

var addToConsole = function(console, value, type) {
    var classes = 'script script-' + type;
    value = value.replace(/&/g, '&amp;')
                 .replace(/>/g, '&gt;')
                 .replace(/</g, '&lt;')
                 .replace(/"/g, '&quot;')
                 .replace(/'/g, '&apos;');
    console.innerHTML += '<p class="' + classes + '">' + value + '</p>';
};

var handleInput = function(event) {
    var key = event.which || event.keyCode;
    if (key == 13) {
        event.preventDefault();
        socket.emit('scriptIn', { script: scriptInput.value });
        addToConsole(scriptResults, scriptInput.value, 'in');
        scriptInput.value = '';
    }
};

var socket = io('http://localhost:8888/ruby'),
    scriptResults = document.getElementById('script-results'),
    scriptInput = document.getElementById('script-input');

socket.on('connect', function() {

    toggleConnectionStatus('<span class="success">Server Connected</span>');

    socket.on('ready', function(data) {
        window.console.log(data);
        addToConsole(scriptResults, data.result, 'welcome');
    });

    socket.on('scriptOut', function(data) {
        window.console.log(data);
        addToConsole(scriptResults, data.error || data.result, 'out');
    });

    scriptInput.addEventListener('keypress', handleInput, false);

    socket.on('disconnect', function() {
        toggleConnectionStatus('<span class="warning">Server Disconnected</span>');
        scriptInput.removeEventListener('keypress', handleInput, false);
    });
});

@@ -1028,17 +1028,16 @@
 ion() %7B%0A
-%0A
     togg
@@ -1109,29 +1109,100 @@
 ');%0A
-%0A
     s
-ocket.on('ready
+criptInput.addEventListener('keypress', handleInput, false);%0A%7D);%0A%0Asocket.on('disconnect
 ', f
@@ -1205,36 +1205,32 @@
 ', function(
-data
 ) %7B%0A    
     window.c
@@ -1221,112 +1221,158 @@
     
-    window.console.log(data
+toggleConnectionStatus('%3Cspan class=%22warning%22%3EServer Disconnected%3C/span%3E'
 );%0A    
-  
-  addToConsole(scriptResults, data.result, 'welcome');%0A    
+scriptInput.removeEventListener('keypress', handleInput, false);%0A
 %7D);%0A%0A
-    
 sock
@@ -1382,17 +1382,13 @@
 on('
-scriptOut
+ready
 ', f
@@ -1407,20 +1407,16 @@
 ) %7B%0A    
- 
-   
 window.c
@@ -1433,20 +1433,16 @@
 (data);%0A
-    
     addT
@@ -1474,118 +1474,33 @@
 ata.
-error %7C%7C data.result, 'out');%0A    %7D);%0A%0A    scriptInput.addEventListener('keypress', handleInput, false);%0A%0A    
+result, 'welcome');%0A%7D);%0A%0A
 sock
@@ -1502,33 +1502,32 @@
 %0Asocket.on('
-disconnec
+scriptOu
 t', function
@@ -1527,24 +1527,28 @@
 unction(
+data
 ) %7B%0A    
     togg
@@ -1543,166 +1543,98 @@
     
-    toggleConnectionStatus('%3Cspan class=%22warning%22%3EServer Disconnected%3C/span%3E');%0A        scriptInput.removeEventListener('keypress', handleInput, false);%0A    %7D
+window.console.log(data);%0A    addToConsole(scriptResults, data.error %7C%7C data.result, 'out'
 );%0A%7D

Fix bug with duplicate socket events on reconnect