Q: How to prevent repeated responses from Node.js server We're running into a problem where we're getting multiple responses sent from our Node server to a web client which are connected by a socket server (socket.io).  By listening with Docklight, I can see that we're really only getting a single response from the serial device, but for some reason the Node server is sending multiples, and they accumulate, so the first time you send a serial command (and it doesn't matter what commands) might only get a couple, next time a couple more, next time a couple more and so on.  So if you run several serial commands, you'll get back lots of multiple responses.
Our environment is Windows 7 64 bit, Node V 4.5.0, serialport V 4.0.1.  However, this needs to run on Windows, Mac & Linux when we're done.  The dev team (me & one other guy) are both fairly new to Node, but otherwise capable developers.
I think what's happening is I'm not using the .flush() & .drain() functions properly and the serialport buffer still contains serial data.  Our proprietary devices return either S>, or <Executed/> prompts when a command has completed, so I store the serial response in a buffer until I see one or the other, then process the data (in this example just providing a boolean response whether the device is responding with one or the other or not).  For example, if I send a <CR><LF> to one of our devices, it should respond with S> (or <Executed/> depending).
The client calls into the server with this:
socket.on('getDeviceConnected', readDeviceResponse);

function readDeviceResponse(isDeviceResponding) {
    console.log('getDeviceConnected');
    console.log(isDeviceResponding);
}
function getDeviceConnected() {
    console.log("Sending carriage return / line feed.");
    socket.emit('getDeviceConnected', '\r\n');
}

And on the server, here's what I'm trying:
    socket.on('getDeviceConnected', function (connectionData) {
        //write over serial buffer before the write occurs to prevent command accumulation in the buffer.
        serialBuffer = '';
        sbeSerialPort.write(connectionData, function (err, results) {
            //since there's no way to tell if the serial device hasn't responded, set a time out to return a false after allowing one second to elapse
            setTimeout(function () {
                console.log('Inside getDeviceConnected setTimeout');
                console.log('Is serial device responding:', isSerialDeviceResponding);
                if (!isSerialDeviceResponding) {
                    console.log('Serial device timed out.');
                    socket.emit('getDeviceConnected', false);
                }
            }, 1000);
            if (err) {
                console.log('Serial port error level:', err);
            }
            if (results) {
                if (results === 2) {
                    console.log('Serial port is responding');
                }
            }
        });

        sbeSerialPort.on('data', function (serialData) {
            isSerialDeviceResponding = true;
            console.log('Does S> prompt exist?', serialData.lastIndexOf('S>'));
            while(!serialData.lastIndexOf('S>') > -1 || !serialData.lastIndexOf('<Executed/>') > -1){
                serialBuffer += serialData;
                break;
            }

            if (isSerialDeviceResponding) {
                socket.emit('getDeviceConnected', true);
                isSerialDeviceResponding = true;
            }
            sbeSerialPort.flush(function (err, results) {
                if (err) {
                    console.log(err);
                    return;
                }
                if(results){
                    console.log('Serial port flush return code:', results);
                }                   
            });
        });

I'm not very sure about the .flush() implementation here, and I've omitted the .drain() part because neither of them seems to do much of anything (assuming they were correctly implemented).
How do I insure that there is no data left in the serialport buffer when the .write() command is complete?  Or do you see other problems with how I'm handling the serial data?
Edit,  Source code up on pastebin.com:
Server.js
Client.js
HTML
