void UdpSocket::handleSend(const boost::system::error_code &err, std::shared_ptr<Buffer> buffer, V4FirstIterator<udp> endpoints) {
	boost::lock_guard<boost::recursive_mutex> guard(commonMutex_);

	if(err && endpoints.hasNext()) {
		udp::endpoint endpoint;
		udp::socket *sock;
		do {
			endpoint = endpoints.next();
			sock = getAppropriateSocket(endpoint);
		} while(!sock->is_open() && endpoints.hasNext());

		if(sock->is_open()) {
			sock->async_send_to(
					boost::asio::const_buffers_1(buffer->getData(), buffer->size()),
					endpoint,
					boost::bind(&UdpSocket::handleSend, shared_from_this(), boost::asio::placeholders::error, buffer, endpoints));
			return;
		}
	}

	asyncSendInProgress_ = false;

	if (!sendqueue_.isEmpty()) {
		asyncSend();
	}
}
boost::system::error_code TcpConnecting::startConnectionAttempt(boost::shared_ptr<TcpSocket> socket,
		V4FirstIterator<tcp> endpoints, boost::system::error_code &ec) {
	if (getSocket().close(ec)) {
		return ec;
	}
	tcp::endpoint endpoint = endpoints.next();
	getSocket().async_connect(endpoint, boost::bind(
			&TcpConnecting::handleConnect, this, socket,
			boost::asio::placeholders::error, endpoints));
	return ec;
}
void TcpConnecting::handleConnect(boost::shared_ptr<TcpSocket> socket,
		const boost::system::error_code &error,
		V4FirstIterator<tcp> endpoints) {
	boost::lock_guard<boost::recursive_mutex> guard(getCommonMutex());

	if (abortRequested) {
		return;
	}

	if (!error) {
		enterConnectedState();
	} else if (endpoints.hasNext()) {
		boost::system::error_code ec;
		if(startConnectionAttempt(socket, endpoints, ec)) {
			enterErrorState(ec.message());
			return;
		}
	} else {
		enterErrorState(error.message());
		return;
	}
}