{"action":"created","issue":{"url":"https://api.github.com/repos/spark/firmware/issues/536","labels_url":"https://api.github.com/repos/spark/firmware/issues/536/labels{/name}","comments_url":"https://api.github.com/repos/spark/firmware/issues/536/comments","events_url":"https://api.github.com/repos/spark/firmware/issues/536/events","html_url":"https://github.com/spark/firmware/issues/536","id":100934058,"number":536,"title":"TCPClient stop() does not work on first connection","user":{"login":"nylen","id":227022,"avatar_url":"https://avatars.githubusercontent.com/u/227022?v=3","gravatar_id":"","url":"https://api.github.com/users/nylen","html_url":"https://github.com/nylen","followers_url":"https://api.github.com/users/nylen/followers","following_url":"https://api.github.com/users/nylen/following{/other_user}","gists_url":"https://api.github.com/users/nylen/gists{/gist_id}","starred_url":"https://api.github.com/users/nylen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nylen/subscriptions","organizations_url":"https://api.github.com/users/nylen/orgs","repos_url":"https://api.github.com/users/nylen/repos","events_url":"https://api.github.com/users/nylen/events{/privacy}","received_events_url":"https://api.github.com/users/nylen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-08-14T05:01:07Z","updated_at":"2015-08-17T07:04:30Z","closed_at":null,"body":"I wrote a simple HTTP server for the Photon.  When a client connects, I write a basic HTTP response in a few lines, then call `client.flush()` and `client.stop()`.\r\n\r\nThis works well, except for one thing:  my code does not disconnect the very first client to connect to the Photon after it boots up, so the connection hangs.  Every connection after the first one works fine.\r\n\r\nHere's my full code:\r\n\r\n```\r\nint led1 = D6; // connected to solid-state relay\r\nint led2 = D7; // the little blue LED built into the board\r\nint buttonLow = D2;\r\nint buttonInput = D1;\r\nint buttonHigh = D0;\r\n\r\nint ledState = HIGH;\r\n\r\nTCPServer server = TCPServer(80);\r\nTCPClient client;\r\nchar addr[16];\r\n\r\nconst int MAX_HTTP_LENGTH = 255;\r\nchar httpBuffer[MAX_HTTP_LENGTH + 1];\r\nint httpLength;\r\n\r\nvoid setup() {\r\n    pinMode(buttonLow, OUTPUT);\r\n    digitalWrite(buttonLow, LOW);\r\n    \r\n    pinMode(buttonInput, INPUT);\r\n    \r\n    // Writing an INPUT mode pin HIGH does not seem to work here,\r\n    // like I hear it does on Arduino\r\n    // so we're using an extra pin\r\n    pinMode(buttonHigh, OUTPUT);\r\n    digitalWrite(buttonHigh, HIGH);\r\n\r\n    pinMode(led1, OUTPUT);\r\n    pinMode(led2, OUTPUT);\r\n    setState(HIGH);\r\n    \r\n    IPAddress localIP = WiFi.localIP();\r\n    sprintf(addr, \"%u.%u.%u.%u\", localIP[0], localIP[1], localIP[2], localIP[3]);\r\n    Spark.variable(\"Address\", addr, STRING);\r\n    Spark.variable(\"Request\", httpBuffer, STRING);\r\n    server.begin();\r\n}\r\n\r\nvoid setState(int state) {\r\n    digitalWrite(led1, state);\r\n    digitalWrite(led2, state);\r\n    ledState = state;\r\n}\r\n\r\nvoid loop() {\r\n    delay(100);\r\n    \r\n    if (digitalRead(buttonInput) == LOW) {\r\n        setState(ledState == HIGH ? LOW : HIGH);\r\n        while (digitalRead(buttonInput) == LOW) {\r\n            delay(100);\r\n        }\r\n    }\r\n    \r\n    client = server.available();\r\n    if (client.connected()) {\r\n        httpLength = 0;\r\n        bool foundNewLine = false;\r\n        while (client.available()) {\r\n            char c = client.read();\r\n            if (c == '\\r' || c == '\\n') {\r\n                foundNewLine = true;\r\n            } else if (!foundNewLine && httpLength < MAX_HTTP_LENGTH) {\r\n                httpBuffer[httpLength++] = c;\r\n            }\r\n        }\r\n        httpBuffer[httpLength] = '\\0';\r\n        client.println(\"HTTP/1.1 200 OK\");\r\n        client.println(\"Content-Type: application/json\");\r\n        client.println(\"Connection: close\");\r\n        client.println();\r\n        client.println(\"\\\"ok\\\"\");\r\n        client.flush();\r\n        client.stop();\r\n        setState(ledState == HIGH ? LOW : HIGH);\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/spark/firmware/issues/comments/131701555","html_url":"https://github.com/spark/firmware/issues/536#issuecomment-131701555","issue_url":"https://api.github.com/repos/spark/firmware/issues/536","id":131701555,"user":{"login":"stevie67","id":4731970,"avatar_url":"https://avatars.githubusercontent.com/u/4731970?v=3","gravatar_id":"","url":"https://api.github.com/users/stevie67","html_url":"https://github.com/stevie67","followers_url":"https://api.github.com/users/stevie67/followers","following_url":"https://api.github.com/users/stevie67/following{/other_user}","gists_url":"https://api.github.com/users/stevie67/gists{/gist_id}","starred_url":"https://api.github.com/users/stevie67/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stevie67/subscriptions","organizations_url":"https://api.github.com/users/stevie67/orgs","repos_url":"https://api.github.com/users/stevie67/repos","events_url":"https://api.github.com/users/stevie67/events{/privacy}","received_events_url":"https://api.github.com/users/stevie67/received_events","type":"User","site_admin":false},"created_at":"2015-08-17T07:04:30Z","updated_at":"2015-08-17T07:04:30Z","body":"@m-mcgowan, I tested this with the newest develop branch and my example now works fine! Thanks for the fix!"}}