class BasicSocket

def buffered=(value)

def buffered=(value)
	super
	
	if ip_protocol_tcp?
		# When buffered is set to true, TCP_NODELAY shold be disabled.
		self.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, value ? 0 : 1)
	end
rescue ::Errno::EINVAL
	# On Darwin, sometimes occurs when the connection is not yet fully formed. Empirically, TCP_NODELAY is enabled despite this result.
rescue ::Errno::EOPNOTSUPP
	# Some platforms may simply not support the operation.
end

def buffered?

def buffered?
	return false unless super
	
	if ip_protocol_tcp?
		return !self.getsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY).bool
	else
		return true
	end
end

def ip_protocol_tcp?

def ip_protocol_tcp?
	local_address = self.local_address
	
	return (local_address.afamily == ::Socket::AF_INET || local_address.afamily == ::Socket::AF_INET6) && local_address.socktype == ::Socket::SOCK_STREAM
end

def readable?

def readable?
	# If we can wait for the socket to become readable, we know that the socket may still be open.
	result = self.recv_nonblock(1, ::Socket::MSG_PEEK, exception: false)
	
	# No data was available - newer Ruby can return nil instead of empty string:
	return false if result.nil?
	
	# Either there was some data available, or we can wait to see if there is data avaialble.
	return !result.empty? || result == :wait_readable
rescue Errno::ECONNRESET, IOError
	# This might be thrown by recv_nonblock.
	return false
end