I am using go for creating a command-line app that tries pings a given host/IP address. I have used the following code in the main function and made the necessary imports/variable declarations outside.

```
package main

import (
    "flag"
    "fmt"
    "net"
    "os"
    "time"

    "golang.org/x/net/icmp"
    "golang.org/x/net/ipv4"
    "golang.org/x/net/ipv6"
)

var icmpType icmp.Type
var isIPv4 = false
var conn *icmp.PacketConn = nil
var start time.Time
var num int

var sequence int

func throwError(err error) {
    fmt.Println(err)
    os.Exit(1)
}

func main() {

    host := "google.com"

    ttl := 64

    ipaddr, err := net.ResolveIPAddr("ip", host)

    if err != nil {
        throwError(err)
    }

    fmt.Println("The host is", host)
    fmt.Println("Address of processing is", ipaddr)

    if ipaddr.IP.To4() != nil {
        conn, err := icmp.ListenPacket("ip4:icmp", "0.0.0.0")
        if err != nil {
            throwError(err)
        }
        conn.IPv4PacketConn().SetTTL(ttl)
        isIPv4 = true
        icmpType = ipv4.ICMPTypeEcho
    } else {
        conn, err := icmp.ListenPacket("ip6:ipv6-icmp", "::")
        if err != nil {
            throwError(err)
        }
        conn.IPv6PacketConn().SetHopLimit(ttl)
        icmpType = ipv6.ICMPTypeEchoRequest
    }

    fmt.Println("isipv4", isIPv4)

    msg := icmp.Message{
        Type: icmpType, Code: 0,
        Body: &icmp.Echo{
            ID: os.Getpid() & 0xffff, Seq: 0,
            Data: []byte(""),
        },
    }

    msgBytes, err := msg.Marshal(nil)
    if err != nil {
        fmt.Println(err)
    }

    _, err = conn.WriteTo(msgBytes, ipaddr)
    if err != nil {
        // error occurs inside this block
        fmt.Println(err)
    }

}

```

Specifically, as far as I was able to figure out, the error occurs after I call the `conn.WriteTo` function. 

The error message is `invalid connection`

Any help would be appreciated. Thanks!