use std::net::SocketAddr;
use std::time::Duration;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpSocket;

#[tokio::main]
async fn main() {
    let addr: SocketAddr = "127.0.0.1".parse().unwrap();
    let _ = connect_and_reset(addr).await;
}

async fn connect_and_reset(addr: SocketAddr) -> Result<(), String> {
    let socket = match TcpSocket::new_v4() {
        Ok(s) => s,
        Err(e) => return Err(e.to_string()),
    };
    let mut stream = match socket.connect(addr).await {
        Ok(s) => s,
        Err(e) => return Err(e.to_string()),
    };
    if let Err(e) = stream
        .write_all(b"GET /test HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n")
        .await
    {
        return Err(e.to_string());
    }
    // cause reset
    stream.set_linger(Some(Duration::from_millis(0))).ok();
    Ok(())
}
