Writing to and reading from socket in an IMAP client
The code looks fine.  Instead of const std::string &amp;command, you can take std::string_view command to avoid unnecessary allocation when the argument is not readily a std::string (e.g., string literals).  std::ostringstream seems more appropriate than std::stringstream here, since you are not reading from it.  Creating a new stream may also be more readable than reusing the stream.

Prior to C++20, str on string streams always make a copy of the buffer, so there isn't much you can do about it.  Since C++20, you can move the underlying buffer by moving the string stream itself:

// since C++20
std::string response = std::move(ss).str();