Are you sure this function isn't going to be called from multiple threads simultaneously? I notice you use `printedData` to anticipate multiple socket closes, but I wonder if those could ever happen at the same time.