public void flush(int maxSize) throws IOException
	{
		int size=4;
		ByteBuffer buffer=ByteBuffer.allocate(maxSize+4);
		buffer.putInt(buffer.capacity());
		//System.out.println("buffer capacity: "+buffer.capacity());
		while(messages.peek()!=null&&size+messages.peek().size()<=buffer.capacity())
		{
			//System.out.println("Messgae written.");
			size+=messages.peek().size();
			buffer.put(messages.pop().bytes());
		}
		buffer.putInt(0,size-4);
		out.write(buffer.array(),0,size);
		//System.out.println("flush size: "+size);
		out.flush();
	}