@@ -552,7 +552,29 @@ private function sendRawRequest(string $host, int $port, string $rawRequest)
552552 @socket_connect ($ this ->socket , $ host , $ port );
553553 $ socketErrorCode = socket_last_error ($ this ->socket );
554554 if ($ socketErrorCode === 115 ) {
555- $ this ->logger ->info ('Bedrock\Client - socket_connect returned error 115, continuing. ' );
555+ $ this ->logger ->info ('Bedrock\Client - socket_connect returned EINPROGRESS, waiting for connection to complete ' );
556+
557+ // Use select to wait for the socket to become writable (indicates connection completion)
558+ $ write = [$ this ->socket ];
559+ $ read = null ;
560+ $ except = null ;
561+
562+ $ selectResult = socket_select ($ read , $ write , $ except , $ this ->connectionTimeout , $ this ->connectionTimeoutMicroseconds );
563+ if ($ selectResult === false ) {
564+ $ socketError = socket_strerror (socket_last_error ());
565+ throw new ConnectionFailure ("Failed to select on socket for host $ host: $ port. Error: $ socketError " );
566+ } elseif ($ selectResult === 0 ) {
567+ throw new ConnectionFailure ("Connection timeout while waiting for EINPROGRESS completion for host $ host: $ port " );
568+ }
569+
570+ // Check if connection completed successfully
571+ $ connectionError = socket_get_option ($ this ->socket , SOL_SOCKET , SO_ERROR );
572+ if ($ connectionError !== 0 ) {
573+ $ socketError = socket_strerror ($ connectionError );
574+ throw new ConnectionFailure ("Connection failed after EINPROGRESS for host $ host: $ port. Error: $ connectionError $ socketError " );
575+ }
576+
577+ $ this ->logger ->info ('Bedrock\Client - EINPROGRESS connection completed successfully ' );
556578 } elseif ($ socketErrorCode ) {
557579 $ socketError = socket_strerror ($ socketErrorCode );
558580 throw new ConnectionFailure ("Could not connect to Bedrock host $ host: $ port. Error: $ socketErrorCode $ socketError " );
0 commit comments