def setup()
      send_bridge_request
      code = nil
			name = nil
			headers = []
			while (line = gets())
				line = line.strip
				if (line == "")
					case code.to_i
					when 100 # 100 Continue, just a ping. Ignore.
						code = name = nil
						headers = []
						next
					when 101 # 101 Upgrade, successfuly got a connection.
						write("HTTP/1.1 100 Continue\r\n\r\n") # let the server know we're still here.
						return self
					when 401 # 401 Access Denied, key wasn't right.
					  close()
					  raise "HTTP BRIDGE error #{code}: host key was invalid or missing, but required."
					when 503, 504 # 503 Service Unavailable or 504 Gateway Timeout, just retry.
						close()
						sleep_time = headers.find {|header| header["Retry-After"] } || 5
						raise RetryError.new("BRIDGE server timed out or is overloaded, wait #{sleep_time}s to try again.", sleep_time)
					else
						raise "HTTP BRIDGE error #{code}: #{name} waiting for connection."
					end
				end
		
				if (!code && !name) # This is the initial response line
					if (match = line.match(%r{^HTTP/1\.[01] ([0-9]{3,3}) (.*)$}))
						code = match[1]
						name = match[2]
						next
					else
						raise "Parse error in BRIDGE request reply."
					end
				else
					if (match = line.match(%r{^(.+?):\s+(.+)$}))
						headers.push({match[1] => match[2]})
					else
						raise "Parse error in BRIDGE request reply's headers."
					end
				end
			end
			return nil
		end
def setup()
      send_bridge_request
      code = nil
			name = nil
			headers = []
			while (line = gets())
				line = line.strip
				if (line == "")
					case code.to_i
					when 100 # 100 Continue, just a ping. Ignore.
						code = name = nil
						headers = []
						next
					when 101 # 101 Upgrade, successfuly got a connection.
						write("HTTP/1.1 100 Continue\r\n\r\n") # let the server know we're still here.
						return self
					when 401 # 401 Access Denied, key wasn't right.
					  close()
					  raise "HTTP BRIDGE error #{code}: host key was invalid or missing, but required."
					when 503, 504 # 503 Service Unavailable or 504 Gateway Timeout, just retry.
						close()
						sleep_time = headers.find {|header| header["Retry-After"] } || 5
						raise RetryError.new("BRIDGE server timed out or is overloaded, wait #{sleep_time}s to try again.", sleep_time)
					else
						raise "HTTP BRIDGE error #{code}: #{name} waiting for connection."
					end
				end
		
				if (!code && !name) # This is the initial response line
					if (match = line.match(%r{^HTTP/1\.[01] ([0-9]{3,3}) (.*)$}))
						code = match[1]
						name = match[2]
						next
					else
						raise "Parse error in BRIDGE request reply."
					end
				else
					if (match = line.match(%r{^(.+?):\s+(.+)$}))
						headers.push({match[1] => match[2]})
					else
						raise "Parse error in BRIDGE request reply's headers."
					end
				end
			end
			return nil
		end
This does the full setup process on the request, returning only
 when the connection is actually available.