         singleHeaders.removeValue(forKey: key)
         multiHeaders.removeValue(forKey: key)
     }
-    
-    ///
-    /// Write a string as a response 
+
+    ///
+    /// Write a string as a response
     ///
     /// - Parameter string: String data to be written.
     ///
     /// - Throws: ???
     ///
     public func write(from string: String) throws {
-        
+
+        if  socket != nil  {
+            try flushStart()
+            try writeToSocketThroughBuffer(text: string)
+        }
+
+    }
+
+    ///
+    /// Write data as a response
+    ///
+    /// - Parameter data: NSMutableData object to contain read data.
+    ///
+    /// - Returns: Integer representing the number of bytes read.
+    ///
+    /// - Throws: ???
+    ///
+    public func write(from data: NSData) throws {
+
         if  let socket = socket {
             try flushStart()
-            try socket.write(from: string)
-        }
-        
-    }
-    
-    ///
-    /// Write data as a response
-    ///
-    /// - Parameter data: NSMutableData object to contain read data.
-    ///
-    /// - Returns: Integer representing the number of bytes read.
-    ///
-    /// - Throws: ???
-    ///
-    public func write(from data: NSData) throws {
-        
-        if  let socket = socket {
-            try flushStart()
-            try socket.write(from: data)
-        }
-        
-    }
-    
+            if  buffer.length + data.length > BUFFER_SIZE  &&  buffer.length != 0  {
+                try socket.write(from: buffer)
+                buffer.length = 0
+            }
+            if  data.length > BUFFER_SIZE {
+                try socket.write(from: data)
+            }
+            else {
+                buffer.append(data)
+            }
+        }
+
+    }
+
     ///
     /// End the response
     ///

 * limitations under the License.
 **/

import Socket

import Foundation

    ///
    private func flushStart() throws {

        if  socket == nil  ||  startFlushed  {
            return
        }

        try writeToSocketThroughBuffer(text: "HTTP/1.1 ")
        try writeToSocketThroughBuffer(text: String(status))
        try writeToSocketThroughBuffer(text: " ")
        var statusText = Http.statusCodes[status]

        if  statusText == nil {
            statusText = ""
        }

        try writeToSocketThroughBuffer(text: statusText!)
        try writeToSocketThroughBuffer(text: "\r\n")

        for (key, value) in singleHeaders {
            try writeToSocketThroughBuffer(text: key)
            try writeToSocketThroughBuffer(text: ": ")
            try writeToSocketThroughBuffer(text: value)
            try writeToSocketThroughBuffer(text: "\r\n")
        }

        for (key, valueSet) in multiHeaders {
            for value in valueSet {
                try writeToSocketThroughBuffer(text: key)
                try writeToSocketThroughBuffer(text: ": ")
                try writeToSocketThroughBuffer(text: value)
                try writeToSocketThroughBuffer(text: "\r\n")
            }
        }

        try writeToSocketThroughBuffer(text: "\r\n")
        startFlushed = true
    }

    ///
    /// Function to write Strings to the socket through the buffer
    ///
    private func writeToSocketThroughBuffer(text: String) throws {
        guard let socket = socket,
              let utf8Data = StringUtils.toUtf8String(text) else {
            return
        }

        if  buffer.length + utf8Data.length > BUFFER_SIZE  &&  buffer.length != 0  {
            try socket.write(from: buffer)
            buffer.length = 0
        }
        if  utf8Data.length > BUFFER_SIZE {
            try socket.write(from: utf8Data)
        }
        else {
            buffer.append(utf8Data)
        }
    }
}
