protected synchronized void flushAndClose( int code, String message, boolean remote )
	{
		if ( flushandclosestate )
		{
			return;
		}
		closecode = code;
		closemessage = message;
		closedremotely = remote;
		
		flushandclosestate = true;
		
		wsl.onWriteDemand( this ); // ensures that all outgoing frames are flushed before closing the connection
		try
		{
			wsl.onWebsocketClosing( this, code, message, remote );
		}
		catch ( RuntimeException e )
		{
			wsl.onWebsocketError( this, e );
		}
		if ( draft != null )
			draft.reset();
		handshakerequest = null;
	}