void ASIOConnectAndHandshake::checkHeaderContents(const std::tr1::shared_ptr<MultiplexedSocket>&connection,
						      bool noDelay,
                                                  unsigned int whichSocket,
                                                  Array<uint8,TCPStream::MaxWebSocketHeaderSize>* buffer,
                                                  const ErrorCode&error,
                                                  std::size_t bytes_received) {
    if (connection) {
        char normalMode[]="HTTP/1.1 101 Web Socket Protocol Handshake\r\n";

        size_t whereHeaderEnds=3;
        for (;whereHeaderEnds+1<bytes_received;++whereHeaderEnds) {
            if ((*buffer)[whereHeaderEnds]=='\n'&&
                (*buffer)[whereHeaderEnds-1]=='\r'&&
                (*buffer)[whereHeaderEnds-2]=='\n'&&
                (*buffer)[whereHeaderEnds-3]=='\r') {
                break;
            }
        }
        if (!memcmp(buffer->begin(),normalMode,sizeof(normalMode)-1/*not including null*/)) {
            if (mFinishedCheckCount==(int)connection->numSockets()) {
                mFirstReceivedHeader=*buffer;
            }
            if (mFinishedCheckCount>=1) {
                boost::asio::ip::tcp::no_delay option(noDelay);
                connection->getASIOSocketWrapper(whichSocket).getSocket().set_option(option);
                mFinishedCheckCount--;
                if (mFinishedCheckCount==0) {
                    connection->connectedCallback();
                }
                if (connection->getStreamType()==Sirikata::Network::TCPStream::RFC_6455) {
                    ptrdiff_t diff = whereHeaderEnds+1;
                    MemoryReference mb(buffer->begin()+diff,bytes_received-diff);
                    MakeASIOReadBuffer(connection,whichSocket,mb, connection->getStreamType());
                }else {
                    // Note we shift an additional 16 bytes, ignoring the required
                    // WebSocket md5 response
                    ptrdiff_t diff = whereHeaderEnds+1+16;
                    if ((ptrdiff_t)bytes_received<diff) {
                        diff = bytes_received;
                    }
                    MemoryReference mb(buffer->begin()+diff,bytes_received-diff);
                    MakeASIOReadBuffer(connection,whichSocket,mb, connection->getStreamType());
                }
            }else {
                mFinishedCheckCount-=1;
            }
        }else {
                connection->connectionFailedCallback(whichSocket,"Bad header comparison "
                                                     +std::string((char*)buffer->begin(),bytes_received)
                                                     +" does not match "
                                                     +std::string(normalMode));
                mFinishedCheckCount-=connection->numSockets();
                mFinishedCheckCount-=1;
        }
    }
    delete buffer;
}
void ASIOConnectAndHandshake::handleResolve(const ASIOConnectAndHandshakePtr& thus,
					    const std::tr1::shared_ptr<MultiplexedSocket>&connection,
                                            const Address&address,
                                            bool no_delay,
                                            const boost::system::error_code &error,
                                            tcp::resolver::iterator it) {
    if (!connection) {
        return;
    }
    if (error) {
        connection->connectionFailedCallback(error);
    }else {
        unsigned int numSockets=connection->numSockets();
        for (unsigned int whichSocket=0;whichSocket<numSockets;++whichSocket) {
            connectToIPAddress(thus,
			       connection,
                               address,
                               no_delay,
                               whichSocket,
                               it,
                               boost::asio::error::host_not_found);
        }
    }

}