       end
     end
 
-    defp format_host(string) do
-      charlist = to_charlist(string)
+    defp domain_or_fallback(host) do
+      case domain_or_ip(host) do
+        {:domain, domain} -> domain
+        {:ip, _ip} -> to_charlist(host)
+      end
+    end
+
+    defp domain_or_ip(host) do
+      charlist = to_charlist(host)
 
       case :inet.parse_address(charlist) do
         {:error, :einval} ->

      tls_opts =
        with "https" <- uri.scheme,
             true <- opts[:certificates_verification] do
          formatted_host =
            case format_host(uri.host) do
              {:domain, domain} -> domain
              {:ip, _} -> to_charlist(uri.host)
            end

          security_opts = [
            verify: :verify_peer,
            cacertfile: CAStore.file_path(),
            depth: 20,
            reuse_sessions: false,
            verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: formatted_host]}
          ]

          Keyword.merge(security_opts, tls_opts)

    end

    defp check_original(%URI{host: host, port: port}, %{original: original} = opts) do
      Map.put(opts, :original_matches, original == "#{domain_or_fallback(host)}:#{port}")
    end

    defp check_original(_uri, opts), do: opts
