/// <summary> /// Register this device at the relay service and return a socket /// for upcoming requests (and their responses). /// </summary> /// <returns>Socket on which a request can be awaited.</returns> public Stream Accept() { bool a; lock (abortedLock) { a = aborted; } if (a) { throw new InvalidOperationException(); } else { string host = LocalHostName; int port = LocalPort; Stream s; bool acceptable; // end of headers found int[] x = new int[3]; byte[] b = new byte[1]; do { s = relay.Connect(LocalHostName, LocalPort); do { byte[] buf = Encoding.UTF8.GetBytes( "POST /" + Domain + " HTTP/1.1\r\n" + "Upgrade: PTTH/1.0\r\n" + "Connection: Upgrade\r\n" + "Host: " + host + "\r\n\r\n"); s.Write(buf, 0, buf.Length); for (int j = 0; j != 12; j = j + 1) { int n = s.Read(b, 0, 1); x[j % 3] = n != 0 ? b[0] : -1; } if ((x[0] == '3') && (x[1] == '0') && (x[2] == '7')) { FindLocation(s, out host, out port); } Find("\r\n\r\n", s, out acceptable); Contract.Assert(acceptable); // test } while (acceptable && ((x[0] == '2') && (x[1] == '0') && (x[2] == '4'))); if (!acceptable || (x[0] != '1') || (x[1] != '0') || (x[2] != '1')) { s.Close(); s = null; } } while (acceptable && ((x[0] == '3') && (x[1] == '0') && (x[2] == '7'))); return(s); } }