<commit_before><?php
	// TODO: Cleanup
	
	function QueryMinecraft( $IP, $Port = 25565 )
	{
		if( !( $Resource = @FSockOpen( 'tcp://' . GetHostByName( $IP ), (int)$Port ) ) )
		{
			throw new Exception( "Can't open connection." );
		}
		
		Socket_Set_TimeOut( $Resource, 2 );
		
		FWrite( $Resource, "\xFE" );
		
		$Data = FRead( $Resource, 1 );
		
		if( $Data[ 0 ] != "\xFF" )
		{
			FClose( $Resource );
			throw new Exception( "Server answered something else." );
		}
		
		$Status = Socket_Get_Status( $Resource );
		
		if( $Status[ 'unread_bytes' ] )
		{
			$Data .= FRead( $Resource, $Status[ 'unread_bytes' ] );
		}
		
		FClose( $Resource );
		
		if( !$Data )
		{
			throw new Exception( "Server did not answer anything." );
		}
		
		$Data = Explode( "\xA7", $Data );
		
		return Array(
			'HostName'   => SubStr( $Data[ 0 ], 1 ),
			'Players'    => (int)Str_Replace( "\x00", "", $Data[ 1 ] ), // Stupid notch-code fixes
			'MaxPlayers' => (int)Str_Replace( "\x00", "", $Data[ 2 ] )
		);
	}
	
	//////////////////////////////////////////////////////////////////////
	
	echo "<pre>";
	
	try
	{
		print_r( QueryMinecraft( 'localhost' ) );<commit_msg>Properly decode received string. (Requires 'iconv')<commit_after><?php
	function QueryMinecraft( $IP, $Port = 25565 )
	{
		if( !( $Resource = @FSockOpen( 'tcp://' . GetHostByName( $IP ), (int)$Port ) ) )
		{
			throw new Exception( "Can't open connection." );
		}
		
		Socket_Set_TimeOut( $Resource, 2 );
		
		FWrite( $Resource, "\xFE" );
		
		$Data = FRead( $Resource, 1 );
		
		if( $Data[ 0 ] != "\xFF" )
		{
			FClose( $Resource );
			throw new Exception( "Server answered something else." );
		}
		
		$Data = FRead( $Resource, 2 );
		
		if( StrLen( $Data ) != 2 )
		{
			FClose( $Resource );
			throw new Exception( "WTF?" );
		}
		
		$Data = UnPack( "n", $Data );
		
		$Data = FRead( $Resource, $Data[ 1 ] * 2 );
		FClose( $Resource );
		
		if( !$Data )
		{
			throw new Exception( "Server did not answer anything." );
		}
		
		$Data = IconV( 'UTF-16BE', 'UTF-8', $Data );
		$Data = Explode( "\xA7", $Data );
		
		return Array(
			'HostName'   => Trim( SubStr( $Data[ 0 ], 0, -1 ) ),
			'Players'    => IntVal( isset( $Data[ 1 ] ) ? $Data[ 1 ] : 0 ),
			'MaxPlayers' => IntVal( isset( $Data[ 2 ] ) ? $Data[ 2 ] : 0 )
		);
	}
	
	//////////////////////////////////////////////////////////////////////
	
	echo "<pre>";
	
	try
	{
		print_r( QueryMinecraft( 'localhost' ) );
{'fraction_non_alphanumeric': 0.24443528441879636, 'fraction_numerical': 0.01731244847485573, 'mean_word_length': 2.18086500655308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3591324', 'n_tokens_mistral': 1042, 'n_tokens_neox': 854, 'n_words': 220}