 
         # Plug the remote session into the terminal.
 
-        # The user types in the terminal, so we send the text to the remote server:
+        # The user types in the terminal, so we send the text to the remote server.
+        # We wait until the first write completes before enabling this.
+        @first_response = true
         @terminal.on 'data',  (data) =>
+            console.log("CLIENT --> SERVER: '#{data}'")
+            if @first_response
+                @first_response = false
+                console.log("scrollbar_nlines = ", @scrollbar_nlines)
+                console.log("@terminal.ybase = ", @terminal.ybase)
+                if @scrollbar_nlines > 0  # reconnect
+                    return
+
             @session.write_data(data)
 
         # The terminal receives a 'set my title' message.
         @terminal.on 'title', (title) => @set_title(title)
 
         # The remote server sends data back to us to display:
+        @scrollbar_nlines = 0
         @session.on 'data',  (data) =>
+            console.log("SERVER --> CLIENT: '#{data}'")
             try
+
                 @terminal.write(data)
+
                 if @scrollbar_nlines < @terminal.ybase
                     while @scrollbar_nlines < @terminal.ybase
                         @scrollbar.append($("<br>"))

        for elt in [$(@terminal.element), @element]
            elt.css
                position : 'relative'
                top : 0
                width: "100%"
        @element.resizable('enable')
        @resize()

            return false

        @element.find("a[href=#paste]").click () =>
            s = "To copy, highlight text then press ctrl+c (or command+c).  Press ctrl+v (or command+v) to paste.  When no text is highlighted, ctrl+c sends the usual interrupt signal.<textarea>#{@value()}</textarea>"
            bootbox.alert(s)
            return false

    value: () =>
        return @terminal.lines.join('\n')

    _init_input_line: () =>

