private function connectToHost($host, $port)
{
$host_key = "{$host}:{$port}";
if (!isset($this->protocols[$host_key])) {
if (!isset($this->sockets[$host_key])) {
$this->sockets[$host_key] = new Socket($host, $port, $this->connectTimeoutMS);
$this->sockets[$host_key]->connect();
}
$this->protocols[$host_key] = new Protocol($this->sockets[$host_key]);
if ($this->database != null) {
$db = $this->selectDB($this->database);
if ($this->username != null) {
$db->authenticate($this->username, $this->password, ['protocol' => $this->protocols[$host_key]]);
}
}
}
return $this->protocols[$host_key];
}