// Replace this funtion with the original one!
function discover_pingback_server_uri($url, $timeout_bytes = 2048) {
    global $wp_version;

    $byte_count = 0;
    $contents = '';
    $headers = '';
    $pingback_str_dquote = 'rel="pingback"';
    $pingback_str_squote = 'rel=\'pingback\'';
    $x_pingback_str = 'x-pingback: ';
    $pingback_href_original_pos = 27;

    extract(parse_url($url));

    if (!isset($host)) {
        // Not an URL. This should never happen.
        return false;
    }

    $path  = (!isset($path)) ? '/'        : $path;
    $path .= (isset($query)) ? '?'.$query : '';
    $port  = (isset($port))  ? $port      : 80;

    // Try to connect to the server at $host
    $fp = @fsockopen($host, $port, $errno, $errstr, 2);
    if (!$fp) {
        // Couldn't open a connection to $host;
        return false;
    }

    // Send the HEAD request
    $request = "HEAD $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress-Checker/$wp_version \r\n\r\n";
    //    ob_end_flush();
    fputs($fp, $request);

    // Let's check for an X-Pingback header first
    while (!feof($fp)) {
        $line = fgets($fp, 512);
        if (trim($line) == '') {
            break;
        }
        $headers .= trim($line)."\n";
        $x_pingback_header_offset = strpos(strtolower($headers), $x_pingback_str);
        if ($x_pingback_header_offset) {
            // We got it!
            preg_match('#x-pingback: (.+)#is', $headers, $matches);
            $pingback_server_url = trim($matches[1]);
            return $pingback_server_url;
        }
        if(strpos(strtolower($headers), 'content-type: ')) {
            preg_match('#content-type: (.+)#is', $headers, $matches);
            $content_type = trim($matches[1]);
        }
    }

    if (!preg_match('#(xml|xhtml|sgml)/#is', $content_type)) {
        // Not an (x)html, sgml, or xml page, no use going further
        return false;
    }

    // Send the GET request
    $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: WordPress/$wp_version \r\n\r\n";
//    ob_end_flush();
    fputs($fp, $request);

    // Let's skip the header
    while (!feof($fp)) {
        $line = fgets($fp, 512);
        if (trim($line) == '') {
            break;
        }
    }

    while (!feof($fp)) {
        $line = fgets($fp, 1024);
        $contents .= trim($line);
        $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote);
        $pingback_link_offset_squote = strpos($contents, $pingback_str_squote);
        if ($pingback_link_offset_dquote || $pingback_link_offset_squote) {
            $quote = ($pingback_link_offset_dquote) ? '"' : '\'';
            $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote;
            $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset);
            $pingback_href_start = $pingback_href_pos+6;
            $pingback_href_end = @strpos($contents, $quote, $pingback_href_start);
            $pingback_server_url_len = $pingback_href_end - $pingback_href_start;
            $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len);
            // We may find rel="pingback" but an incomplete pingback URI
            if ($pingback_server_url_len > 0) {
                // We got it!
                return $pingback_server_url;
            }
        }
        $byte_count += strlen($line);
        if ($byte_count > $timeout_bytes) {
            // It's no use going further, there probably isn't any pingback
            // server to find in this file. (Prevents loading large files.)
            return false;
        }
    }

    // We didn't find anything.
    return false;
}