@Override
    public void run()
    {
        resetFlags();
        
        try
        {
            this.in = this.socket.getInputStream();

            // attempt to start a WebSocket session - failure caught in error handler
            String responseString = startSession(in);
            sendResponse(responseString);

            // the main connection loop takes the remaining message input
            while(true)
            {

                parseFrame();

                // close out of the loop if we are to close the WebSocket
                if (closing)
                {
                    System.out.println("Closing WebSocket");
                    sendResponse(response);
                    break;
                }
                
                // if this is the last frame of the message; return the response and then
                // reset flags for new message
                if (FIN) {
                    if (controlFrame)
                    {
                        WSControlHandler.respond(this);
                    }
                    else
                    {
                        server.handler.response(this);
                        // System.out.println(new String(response.getResponse()));
                        System.out.println("Reset all flags as we've received the last frame of the current message.");
                        resetFlags();
                    }
                }

            }
            
        }
        catch (SWSSException swssue)
        {
            // the HTTP Upgrade failed
            System.out.println("Upgrade Error: " + swssue.getMessage());
        }
        catch (IOException ioe)
        {
            // anything
            System.out.println("ERROR: " + ioe.getMessage());
        }
        catch (InterruptedException ie)
        {
            System.out.println("InterruptedException: " + ie.getMessage());
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
        
        System.out.println("Shutting down the request");
        this.server.handler.onClose(this);
        
    }