// socket.wren (Corrected) foreign class Socket { // CORRECTED: Changed 'new_' to 'new' to match the standard convention. construct new() {} foreign connect(host, port, callback) foreign listen(host, port, backlog) foreign accept(callback) foreign read(bytes) foreign close() foreign isOpen foreign remoteAddress foreign remotePort // Implemented in Wren write(data, callback) { write_(data, callback) } readUntil(delimiter, callback) { var buffer = "" var readChunk readChunk = Fn.new { this.read(4096) { |err, data| if (err) { callback.call(err, null) return } buffer = buffer + data var index = buffer.indexOf(delimiter) if (index != -1) { var result = buffer.substring(0, index + delimiter.count) callback.call(null, result) } else { // Delimiter not found, read more data. readChunk.call() } } } // Start reading. readChunk.call() } // Private foreign method for writing foreign write_(data, callback) }