boost::shared_ptr<cql::cql_connection_t>
cql::cql_session_impl_t::allocate_connection(
    const boost::shared_ptr<cql_host_t>& host)
{
    if (!increase_connection_counter(host)) {
        throw cql_too_many_connections_per_host_exception();
    }

    boost::shared_ptr<cql_promise_t<cql_future_connection_t> > promise(
        new cql_promise_t<cql_future_connection_t>());

    boost::shared_ptr<cql_connection_t> connection(_client_callback());

    connection->set_credentials(_configuration->credentials());
    connection->connect(host->endpoint(),
                        boost::bind(&cql_session_impl_t::connect_callback, this->shared_from_this(), promise, ::_1),
                        boost::bind(&cql_session_impl_t::connect_errback,  this->shared_from_this(), promise, ::_1, ::_2));
    connection->set_keyspace(_keyspace_name);

    boost::shared_future<cql_future_connection_t> shared_future = promise->shared_future();
    shared_future.wait();

    if (shared_future.get().error.is_err()) {
        decrease_connection_counter(host);
        throw cql_connection_allocation_error(
                ("Error when connecting to host: " + host->endpoint().to_string()).c_str());
        connection.reset();
    }

    return connection;
}
void
cql::cql_client_pool_impl_t::add_client(const std::string&                        server,
                                        unsigned int                              port,
                                        cql::cql_client_t::cql_event_callback_t   event_callback,
                                        const std::list<std::string>&             events,
                                        const std::map<std::string, std::string>& credentials)
{
    if (_client_callback) {
        std::auto_ptr<cql::cql_client_pool_impl_t::client_container_t> client_container(new cql::cql_client_pool_impl_t::client_container_t(_client_callback()));
        client_container->client->connect(server,
                                          port,
                                          boost::bind(&cql_client_pool_impl_t::connect_callback, this, _1),
                                          boost::bind(&cql_client_pool_impl_t::connect_errback, this, _1, _2),
                                          event_callback,
                                          events,
                                          credentials);
        _clients.push_back(client_container.release());
    }
}