         private let mode: SendMode
         private let fileManager = FileManager.default
         private var files: [String] = Array()
+        private var seenEnd: Bool = false
+        private var sentEnd: Bool = false
+        private var closed: Bool = false
         
         init(_ mode: SendMode) {
             self.mode = mode

                    }
                    ctx.write(data: self.wrapOutboundOut(.end(nil))).whenComplete { r in
                        assertSuccess(r)
                        ctx.close().whenComplete { r in
                            assertSuccess(r)
                        }
                    }
                case "/head":
                    var head = HTTPResponseHead(version: req.version, status: .ok)

        public func channelReadComplete(ctx: ChannelHandlerContext) {
            ctx.flush().whenComplete {
                assertSuccess($0)
                self.maybeClose(ctx: ctx)
            }
        }

        // We should only close the connection when the remote peer has sent the entire request
        // and we have sent our entire response.
        private func maybeClose(ctx: ChannelHandlerContext) {
            if sentEnd && seenEnd && !closed {
                closed = true
                ctx.close().whenComplete { r in
                    assertSuccess(r)
                }
            }
        }
    }
