private function readFromSocket(ApiClient $apiClient, HttpRequest $request, $socket) {
		$inBody = false;
		$responseMessage = '';
		$chunked = false;
		$chunkLength = false;
		$maxTime = $this->getStartTime() + $apiClient->getConnectionTimeout();
		$contentLength = -1;
		$endReached = false;
		while ($maxTime > time() && !feof($socket) && !$endReached) {
			if ($inBody === false) {
				$line = $this->readLineFromSocket($apiClient, $request, $socket, 2048);
				if ($line == "\r\n") {
					$inBody = true;
				} else {
					$parts = explode(':', $line, 2);
					if (count($parts) == 2) {
						$headerName = trim(strtolower($parts[0]));
						$headerValue = trim(strtolower($parts[1]));
						if ($headerName === 'transfer-encoding' && $headerValue == 'chunked') {
							$chunked = true;
						}
						if ($headerName === 'content-length') {
							$contentLength = (int)$headerValue;
						}
					}
				}
				$responseMessage .= $line;
			} else {
				// Check if we can read without chunks
				if (!$chunked) {
					$readBytes = 4096;
					if ($contentLength > 0) {
						$readBytes = $contentLength;
					}
					$tmp = $this->readContentFromSocket($apiClient, $request, $socket, $readBytes);
					$responseMessage .= $tmp;
					if (strlen($tmp) == $readBytes) {
						$endReached = true;
						break;
					}
				} else {
					// Since we have not set any content length we assume that we need to read in chunks.

					// We have to read the next line to get the chunk length.
					if ($chunkLength === false) {
						$line = trim(fgets($socket, 128));
						$chunkLength = hexdec($line);
					} else if ($chunkLength > 0) {
						// We have to read the chunk, when it is greater than zero. The last one is always 0.

						$responseMessage .= $this->readContentFromSocket($apiClient, $request, $socket, $chunkLength);

						// We skip the next line break.
						fseek($socket, 2, SEEK_CUR);
						$chunkLength = false;
					} else {
						// The chunk length must be zero. Hence we are finished and we can stop.

						$endReached = true;
						break;
					}
				}
			}
		}

		if (feof($socket) || $endReached) {
			return $responseMessage;
		} else {
			throw new ConnectionException(null, $request->getLogToken(),
					'The remote server did not respond within ' . $apiClient->getConnectionTimeout() . ' seconds.');
		}
	}