{"record":{"id":"a456d8d5d2f3ad04","repo":"jstedfast/MailKit","slug":"failed-to-connect-to-0-1-2-socks5client","errorCode":null,"errorMessage":"Failed to connect to {0}:{1}: {2}","messagePattern":"Failed to connect to (.+?):(.+?): (.+?)","errorType":"exception","errorClass":"ProxyProtocolException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Proxy/Socks5Client.cs","lineNumber":344,"sourceCode":"\t\t\t\tbreak;\n\t\t\tcase Socks5AddressType.IPv4:\n\t\t\t\taddr = ip!.GetAddressBytes ();\n\t\t\t\tBuffer.BlockCopy (addr, 0, buffer, n, addr.Length);\n\t\t\t\tn += 4;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tbuffer[n++] = (byte) (port >> 8);\n\t\t\tbuffer[n++] = (byte) port;\n\n\t\t\treturn buffer;\n\t\t}\n\n\t\tint ProcessPartialConnectResponse (string host, int port, byte[] buffer)\n\t\t{\n\t\t\tVerifySocksVersion (buffer[0]);\n\n\t\t\tif (buffer[1] != (byte) Socks5Reply.Success)\n\t\t\t\tthrow new ProxyProtocolException (string.Format (CultureInfo.InvariantCulture, \"Failed to connect to {0}:{1}: {2}\", host, port, GetFailureReason (buffer[1])));\n\n\t\t\t// +-----+-----+-------+------+----------+----------+\n\t\t\t// | VER | REP |  RSV  | ATYP | BND.ADDR | BND.PORT |\n\t\t\t// +-----+-----+-------+------+----------+----------+\n\t\t\t// |  1  |  1  | X'00' |  1   | Variable |    2     |\n\t\t\t// +-----+-----+-------+------+----------+----------+\n\t\t\tvar addrType = (Socks5AddressType) buffer[3];\n\n\t\t\tswitch (addrType) {\n\t\t\tcase Socks5AddressType.Domain: return 4 + 1 + buffer[4] + 2;\n\t\t\tcase Socks5AddressType.IPv6: return 4 + 16 + 2;\n\t\t\tcase Socks5AddressType.IPv4: return 4 + 4 + 2;\n\t\t\tdefault: throw new ProxyProtocolException (\"Proxy server returned unknown address type.\");\n\t\t\t}\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Connect to the target host.","sourceCodeStart":326,"sourceCodeEnd":362,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Proxy/Socks5Client.cs#L326-L362","documentation":"When the SOCKS5 CONNECT reply's status byte (REP) is not 0x00, ProcessPartialConnectResponse throws ProxyProtocolException formatted with the requested host, port, and a textual reason from GetFailureReason (e.g. general failure, connection not allowed, network unreachable, host unreachable, connection refused, TTL expired, command not supported, address type not supported). The proxy itself reported that the proxied connection failed.","triggerScenarios":"Socks5Client.Connect/ConnectAsync where the proxy cannot or will not reach the target host:port — target down, proxy egress blocked by policy, unsupported command, or unresolvable target from the proxy's network.","commonSituations":"Target service down or moved, corporate proxy ACLs blocking the destination, trying an SMTP/IMAP port the proxy forbids, DNS not working on the proxy host, or remote host refuses connections from the proxy's IP (allowlist lacks proxy IP).","solutions":["Read the embedded failure reason: for 'connection refused'/'host unreachable' fix the target service or network path, for 'not allowed' fix proxy ACLs","If the target allowlists clients, add the proxy server's egress IP","Confirm the target host/port by testing connectivity from the proxy machine itself","Check the client is using CONNECT (the only command MailKit sends) and the port is what the target expects"],"exampleFix":"// before\nawait proxy.ConnectAsync(proxyHost, 1080, \"internal-db\", 1433); // blocked by proxy ACL\n// after\nawait proxy.ConnectAsync(proxyHost, 1080, \"internal-db.example.com\", 1433); // allowed FQDN, or request ACL change","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    await socks5.ConnectAsync(proxyHost, proxyPort, target, targetPort);\n} catch (ProxyProtocolException ex) {\n    log.Warn(\"SOCKS5 CONNECT to {0} refused: {1}\", target, ex.Message);\n    // branch on the reason text / retry via alternate egress or alert ops\n}","preventionTips":["Keep proxy ACLs in sync with all destinations the app needs","Add proxy egress IPs to target allowlists","Monitor target availability from the proxy network, not just locally"],"tags":["socks5","proxy","connection"],"backgroundTag":"connection-refused","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}